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 Multiple datasources Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Multiple datasources" Watch "Multiple datasources" New topic
Author

Multiple datasources

Shamu Somasundaram
Ranch Hand

Joined: Aug 25, 2004
Posts: 41
Hi,
In our application, we are using two datasources. One for writing to database and another for reading. Now in Hibernate how can I create the session factory for two datasources and how will I determine which data source has to be used for getting the connection?
Normally what will be the approach for such a scenario?
Please give your inputs.
Thanks in advance.
Shanmugavel.
Scott Johnson
Ranch Hand

Joined: Aug 24, 2005
Posts: 518
If you have two datasources, you'll need to configure Hibernate to create two SessionFactory objects -- one for each datasource.

Having two SessionFactory objects will have some side effects. Each SessionFactory will have it's own caches which will use more memory. Also, changes written to one SessionFactory will not be reflected in the other SessionFactory's cache causing some stale data problems if you aren't careful.
Kelvin Kelvin
Greenhorn

Joined: Apr 04, 2006
Posts: 1
but how can we configure two session-facory in hibernate.cfg.xml?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

"Kelvin Kelvin"

Welcome to JavaRanch! Please update your display name so it matches our Naming Policy. You can do this here.

Thanks!


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Originally posted by Kelvin Kelvin:
but how can we configure two session-facory in hibernate.cfg.xml?


You can use more than one configuration file. For example:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Multiple datasources
 
Similar Threads
reg: datasource - very very urgent
Creating datasources using JMX in WAS5.1
Splitting an XML file
Connection Pooling
DataSource configuration