-->
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.  [ 3 posts ] 
Author Message
 Post subject: Same SessionFactory against multiple databases
PostPosted: Tue Apr 29, 2008 9:20 am 
Newbie

Joined: Tue Apr 29, 2008 9:15 am
Posts: 4
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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 29, 2008 11:16 am 
Senior
Senior

Joined: Wed Sep 19, 2007 9:31 pm
Posts: 191
Location: Khuntien (Indonesia)
no. you can do that. One session factory for one database only


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 07, 2008 3:44 am 
Beginner
Beginner

Joined: Thu Sep 30, 2004 4:17 am
Posts: 21
Can anyone from the Hibernate development team answer this question?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.