Reputation: 5029
I set up database connection in persistence.xml including server url and username/password. However, if I want to switch between development environment (dev/qa/stg/prod) is there a better way to change the configuration instead of manually modify persistence.xml?
I am using jdbc connection to call stored procedure together with hibernate stuff. The jdbc connection is more flexible. It connects to different database depending on an environment variable DEV_ENV.
// DEV DB
if (env.equalsIgnoreCase("dev")) {
connectionURL = "jdbc:oracle:thin:@ldap://mdaoid.mdanderson.org:389/risdev3, cn=OracleContext,dc=mdacc,dc=tmc,dc=edu";
user = "ristore_owner";
pass = "ristoreowner987";
}
// QA DB
else if (env.equalsIgnoreCase("qa")) {
connectionURL = "jdbc:oracle:thin:@ldap://mdaoid.mdanderson.org:389/risdev3, cn=OracleContext,dc=mdacc,dc=tmc,dc=edu";
user = "ristore_owner_qa";
pass = "ristore987q";
}
How do I configure hibernate db connection in the same way, pick up environment variable and decide which db to connect to on the fly?
EDIT: People showed me how to do it with a war which picks up the db information from tomcat server. However, mine is a java application (jar) which I don't deploy. I set it up as a cron job to run the jar on weekly basis.
Upvotes: 1
Views: 693
Reputation: 2676
I see that you provide the environment somehow. My suggestion is to use a build tool (like Maven, Gradle, etc.) through which you can give as arguments the actual environment like UAT, Dev, Prod.
Then you can have multiple configuration files like application-dev.xml, application-uat.xml. During build you use the build tool to load the approppiate configuration file.
As a reference for doing this in Maven you can have a look here.
Upvotes: 0
Reputation: 192
Create two xml config files. persistenceDev.xml and persistenceQA.xml
Add url, user, and password for each environment.
Note: The only thing you change is the config file name. I would provide is a command line argument or a static final variable. Say, assuming 0 = dev, 1 = qa.
String configFileName = (args[0] == 0) ? persistenceDev.xml: persistenceQA.xml;
Your code after getting the config file does not change.
Upvotes: 1