Announcement Announcement Module
Collapse
No announcement yet.
Where to specify database? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Where to specify database?

    I am using the following datasource bean in Spring running in Tomcat 6. What is the preferred way to specify the database so that it is easy to configure a different db for your dev and prod environments?

    The way I have it here would require that each time I build a WAR file for production I would have to remember to first edit the xml file and change the database.

    Code:
     <bean id="dataSource"  class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName"
                value="com.mysql.jdbc.Driver" />
            <property name="url"  value="jdbc:mysql://localhost:3306/mydb?autoReconnect=true" />
            <property name="username" value="me" />
            <property name="password" value="pass" />
            <property name="initialSize" value="10" />
            <property name="maxActive" value="100" />
    	<property name="maxIdle" value="-1" />
    	<property name="maxWait" value="-1" />
        </bean>
    Thanks for your help

    John

  • #2
    I'm using following idea:
    Code:
        <bean id="serverPropertiesPlaceholder" 
              class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" abstract="true"> 
            <property name="ignoreResourceNotFound" value="true"/> 
            <property name="ignoreUnresolvablePlaceholders" value="true"/> 
     
            <!-- This resource is not defined by default. It should be provided by end configuration file --> 
            <property name="location" value="classpath:/server-settings.properties"/> 
        </bean> 
     
        <!-- This bean defines default value for all properties in this XML file. --> 
        <bean id="hibernatePropertyPlaceholderConfigurer" parent="serverPropertiesPlaceholder"> 
            <property name="properties"> 
                <props> 
                    <prop key="hibernate.jdbc.url">jdbc:mysql://host/db</prop> 
                    <prop key="hibernate.jdbc.username">default</prop> 
                    <prop key="hibernate.jdbc.password">default</prop> 
                </props> 
            </property> 
        </bean> 
     
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
            <property name="driverClassName" value="com.mysql.jdbc.Driver"/> 
            <property name="url" value="${hibernate.jdbc.url}?autoReconnect=true&amp;zeroDateTimeBehavior=convertToNull&amp;useUnicode=true&
            <property name="username" value="${hibernate.jdbc.username}"/> 
            <property name="password" value="${hibernate.jdbc.password}"/> 
        </bean>
    I have a abstract properties placeholder that takes properties from server-settings.properties file in root of classpath.
    After that I defilne implementations of this bean: hibernatePropertyPlaceholderConfigurer with default values. After that I has link to these properties in other beans.

    It work in following order: if settings file exist, properties from this file will be taken. If not - default properties fill be taken.

    On my test enironment (and test-cases) I have one settings file. On work server I have another (with always on server in classpath).

    Comment


    • #3
      Thank you! Wow. That was the perfect solution i was looking for. I followed your idea and it appears to be working great. Thank you so much

      Comment

      Working...
      X