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.
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.
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.
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