Hello...
I need to connect to different databases running on a same MySql Server from my application. How can I do it using Hibernate. My application should connect to different databases based on the user login. It will be difficult to use multiple data sources and multiple SessionFactories , since the number of databases is more. So my plan is to have a single connection pool with a JNDI datasource. This datasource will be used for building the SessionFactory. So I will be having only one SessionFactory. Is it possible to use this SessionFactory against multiple databases ???
I will be having the following Configuration in hibernate-cfg.xml
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="connection.datasource">java:/MySqlHrDS</property>
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="transaction.factory_class">org.hibernate.transaction.CMTTransactionFactory</property>
<property name="transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>
<property name="connection.release_mode">auto</property>
<property name="transaction.flush_before_completion">true</property>
<property name="transaction.auto_close_session">true</property>
For the datasource "MySqlHrDS" , I will be using a dummy database as part of database url.
Is it possible to switch the database once SessionFactory is configured with above properties????
Will there be any issues , if I use the below code to change the database at run time??? Is this the right way ????
// Configure SessionFactory with above properties.
// datasource will be pointing to dummy database
// Whenever I get Hibernate session, I will call the connection method and will change the catelog
session = HibernateSessionFactory.currentSession();
session.connection().setCatalog("new_database");
Any pointers / guidance will be really helpful.
Thanks & Regards
unnis
|