How to connect to various databases (development / testing / production) transparently with sleep mode

I have several different databases for different environments for connecting my application. They are constant for each installation, but differ from each other. In other words, there is

  • development environment that connects to the development database
  • a test environment that connects to a test database and finally
  • Production environment with its own database

Hibernation is used via annotations, and the code does not know in which environment it is running. All databases are based on mySQL, but differ in URLs, username and password.

Currently, I removed hibernate.cfg.xml from the application and moved it to the home directory of the server application user, but this does not seem to be a very good solution for security reasons, and since this led to the need to manually update any changed mappings when updating the environment.

I really hope there is a better solution, but I cannot find it.

+4
source share
4 answers

You can link hibernate.cfg.xml to .jar, or you can use the JPA (persistence.xml) approach instead, which allows you to have different โ€œduration unitsโ€ that you can then choose based on any variable, d like (file properties 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 persistence.xml file.

+3
source

Use placeholders for the URL, user, and password in the configuration file and replace these placeholders with the actual values โ€‹โ€‹when creating the application (using ant, Maven, or whatever you use to create your application). The build process itself can take these values โ€‹โ€‹from the profile (if Maven is used) or from environment variables or from command line arguments.

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

+3
source

I think the point here is not to restore the application 3 times. I think you can use different data sources that will define different URLs of your database server in different environments (dev / test / prod). A datasource file is an xml file that should be located outside of your artifact (ear / war or whatever you create).

Thus, you always provide the same ears and different data sources for different environments.

+1
source

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 property files, namely etc / dev / pejl.properties , etc / test / pejl.properties , etc. / prod / payl.properties .

Then, using my ant script, I create 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 the 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> 

etc.

+1
source

All Articles