Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiple datasources

 
Shamu Somasundaram
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but how can we configure two session-facory in hibernate.cfg.xml?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Kelvin Kelvin"

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

Thanks!
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic