• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Configure Database dynamically in Hibernate

 
Venkata Sirish
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Venkata Sirish
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic