How to connect to different databases (development/test/production) transparently with hibernate

前端 未结 4 1627
广开言路
广开言路 2021-01-01 01:35

I have several different databases for different environments to connect my application to. These are constant per installation but differ between them. In other words there

相关标签:
4条回答
  • 2021-01-01 02:14

    You can bundle the hibernate.cfg.xml in a .jar or you could use a JPA (persistence.xml) approach instead which allows you to have different "persistence units" that you can then choose based on any variable you'd like (a properties file in your home for example). See http://docs.jboss.org/hibernate/entitymanager/3.6/reference/en/html/configuration.html#setup-configuration-packaging for an example of a persistence.xml.

    0 讨论(0)
  • 2021-01-01 02:18

    I think that the point here is not to rebuild the application 3 times. I think you can use different data sources that will define different urls of you data base server in different environment (dev/test/prod). The datasource file is an xml file that should reside externally to your artifact (ear/war or whatever you build).

    So you provide always the same ear and different data source files for different environments

    0 讨论(0)
  • 2021-01-01 02:33

    This is how I do it. My app-config file

     <!-- Hibernate MySql SessionFactory -->
     <bean id="mySqlSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
          p:dataSource-ref="mySqlDataSource">
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
       .....
            </props>
        </property>
        <property name="configLocation" value="WEB-INF/hibernate.cfg.xml"/>
    </bean>
    

    Then I have these variables defined in different properties files namely etc/dev/pejl.properties, etc/test/pejl.properties and etc/prod/pejl.properties.

    Then using my ant script I build for development ...

    <target name="deploydevwar" depends="build" description="Deploy application as a WAR file">
         <copy overwrite="true" todir="${web.dir}/WEB-INF/classes">
            <fileset dir="${etc.dir}/dev">
                <include name="*.properties" />               
            </fileset>
            </copy>
        <war destfile="${name}.war"
             webxml="${web.dir}/WEB-INF/web.xml">
            ...
       </war>
        <copy todir="${deploy.path}" preservelastmodified="true">
           .. 
        </copy>
    
         <copy overwrite="true" todir="${appserver.home}">
             ...
         </copy>            
    </target>
    

    for test..

    <target name="deploytestwar" depends="build" description="Deploy application as a WAR file">
         <copy overwrite="true" todir="${web.dir}/WEB-INF/classes">
            <fileset dir="${etc.dir}/test">
                <include name="*.properties" />               
            </fileset>
    

    and so on..

    0 讨论(0)
  • 2021-01-01 02:34

    Use placeholders for the url, user and password in the config file, and replace those placeholders with actual values when building the application (using ant, Maven, or whatever you use to build your application). The build process itself could take those values from a profile (if using Maven), or from environment variables, or from command-line arguments.

    You just need to build the application three times, with different parameters : once for each target environment.

    0 讨论(0)
提交回复
热议问题