File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Configure Database dynamically in Hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Reply Bookmark "Configure Database dynamically in Hibernate" Watch "Configure Database dynamically in Hibernate" New topic
Author

Configure Database dynamically in Hibernate

Venkata Sirish
Ranch Hand

Joined: Apr 09, 2007
Posts: 112
Hi ranchers,

My web application needs to support two databases either MS-SQL or Oracle. My application will be deployed on Weblogic Server. Users will be creating a datasource (either MS-SQL or Oracle) in Weblogic Server.

As my application need to support many databases i have decided to use hibernate, so that i can code to HQL rather than SQL.

Can i configure Hibernate Service based on the datasource created in Weblogic so that i can configure the hibernate dialect and other stuff dynamically.

My requirement is that not to support multiple databases at a time, but rather my application should configure (connect) a single database dynamically using hibernate.

Thanks in Advance for your time,
Venkata Guru
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17231
    
    1

Well there are a lot of options.

one you can always instantiate your SessionFactory in code, so in code you could say read a .properties file that has the datasource information. Then load that info when creating the SessionFactory.

You could just have the datasource name that is put into jndi in the App Server always be the same name, so all you need in your Hibernate config file is pointing to the jndi name, then just swap out the datasource definition in the App Server.

You could use Spring to have different xml config files that define the Datasource bean and the Factory and inject them into your DAO classes.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Venkata Sirish
Ranch Hand

Joined: Apr 09, 2007
Posts: 112
Thanks mark for the reply. I have one more clarification. What about the dialect hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect which needs to be specific to a database.

Is there any way, this can also be made dynamic.

I have searched a lot over the internet, but i could not find a solution to it.

Thanks,
Venkat
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17231
    
    1

Well the thing is, a SessionFactory is one to one with a database datasource/connection so just one dialect. So you would declare different SessionFactories for each datasource. You really can't dynamically switch out a dialect in the middle of your app. You can when you create your Configuration object, which you pass to the SessionFactory, but once the SessionFactory is created, that instance is immutable. It cannot be changed.

Mark
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Configure Database dynamically in Hibernate
 
Similar Threads
How to connect multiple databases at a time using Hibernate
DAO pattern and server's DataSource approach
Connecting to multiple DataBase
ID Generation using Identity
web application works fine with us and canada time