-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: Pattern for externalizing database settings?
PostPosted: Sat Mar 08, 2008 10:40 pm 
Newbie

Joined: Sat Mar 08, 2008 10:15 pm
Posts: 1
Location: San Francisco, CA, USA
Hi,

I'm wondering if anyone knows of a way to externalize the database settings that are typically stored in the hibernate config file; i.e. these values:

<property name="connection.url">jdbc:mysql://localhost/db</property>
<property name="connection.username">myuser</property>
<property name="connection.password">mypassword</property>

I'd like to be able to pull those values out and put them, say, in a separate configuration or properties file, or as environment variables or system properties, etc.

Here's the rationale. Our scenario (which I can't imagine is all that uncommon) is that we are deploying our entire application as a single EAR file. Within that EAR is, of course, the hibernate.cfg.xml file which includes the above values. The EAR is generated on a build machine and (being Java and all) is build-once, run-anywhere. Meaning that the same build machine--which builds from our single source code repository--build all the EARs for every environment: development, QA, integration testing, production, etc.

Needless to say, those different environments require different database settings. So as it stands now, here's what happens if I want to build an EAR for, say, QA:

1. Check out hibernate.cfg.xml from repository
2. Ensure that the database-related properties are set for QA
3. Check hibernate.cfg.xml back into the repository
4. Run the build script on the build machine (which checks out the latest source code, including my recent property changes, compiles, and creates the EAR)

Needless to say, that's suboptimal, and dangerous (what if someone else is simultaneously doing a production build?)

I'd think that I could externalize the entire hibernate.cfg.xml file, but that also seems bad. We want to distribute all of the other settings identically across environments, so having copies of a nearly identical file laying around on different machines--with no central place to make changes--is not good.

So, is there a way to have Hibernate read in values that are normal in the config file from another location?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 10, 2008 9:20 am 
Newbie

Joined: Fri Mar 07, 2008 5:32 pm
Posts: 6
This is exactly what I need to now.

Someone have any idea? Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 10, 2008 9:36 am 
Beginner
Beginner

Joined: Fri Jun 25, 2004 11:47 am
Posts: 34
Hi,

Why don't you try to use ant?
For example, in your hibernate config file you would have:

Code:
<property name="connection.url">@database.jdbc.url@</property>


Then you define a property file having a property called:
Code:
database.jdbc.url=jdbc:mysql://localhost/db


Then you have an ant target so made:


Code:
<replace dir="${root.distribution.dir}\config\Project" token="@database.jdbc.url@"  value="${database.jdbc.url}"/>


And finally, you would call your script so:

ant -f build.properties -propertyfile ../install.properties


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 10, 2008 11:01 am 
Senior
Senior

Joined: Fri Jun 01, 2007 12:41 pm
Posts: 121
Check my posting in
http://forum.hibernate.org/viewtopic.php?t=983822&highlight=

You can use Configuration to set the hibernate properties while building session factory.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 10, 2008 2:02 pm 
Newbie

Joined: Fri Mar 07, 2008 5:32 pm
Posts: 6
knoll wrote:
Hi,

Why don't you try to use ant?
For example, in your hibernate config file you would have:

Code:
<property name="connection.url">@database.jdbc.url@</property>


Then you define a property file having a property called:
Code:
database.jdbc.url=jdbc:mysql://localhost/db


Then you have an ant target so made:


Code:
<replace dir="${root.distribution.dir}\config\Project" token="@database.jdbc.url@"  value="${database.jdbc.url}"/>


And finally, you would call your script so:

ant -f build.properties -propertyfile ../install.properties



Correct me if I'm wrong, but if I do that you said, I still needing to build my project right?
My idea is: build one time, later I just need to put a configuration file in a specific directory, and do deploy. So if I'm changing from a dev environment to another, i.e. production, then I just need to configure that configuration file and then deploy my project.

I've already tried this:
Code:
protected SessionFactory factory;
-----------
File externalFile = new File("/external.cfg.xml");
factory = new Configuration().configure(externalFile).buildSessionFactory();


But looks that hibernate doesn't read every time that I change my /external.cfg.xml file..


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 10, 2008 3:41 pm 
Beginner
Beginner

Joined: Fri Jun 25, 2004 11:47 am
Posts: 34
Hi,

I'm currently using Hibernate in Jboss AS.
In this context, you can deploy hibernate as a MBean and then changed these property dynamically via the JMX console. So far, was the most elegant solution I've seen.

About the code you send, I know not enough for this part from Hibernate API.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 10, 2008 4:56 pm 
Newbie

Joined: Fri Mar 07, 2008 5:32 pm
Posts: 6
I'm using Weblogic in this case.. but I still using JBoss for local testing.

Thanks..


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.