This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

multiple DB connections using hibernate

 
mano ranjan
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Is there a way to create different db connection in runtime using hibernate.
i.e i have 3 databases and i need three connections based on a particular condition...if there is a way please let me know

thanks
mano
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to create three distinct SessionFactories if you have three distinct DataSources. Any conditional code to select which source you use would normally be in your DAO factory class (assuming you are using thge DAO parrten)
 
mano ranjan
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks for your reply.We have three different databases.So which is the easiest way to get the data source ,by having 3 different hibernate.cfg.xml or by getting 3 different data sources form app server using jndi.

thanks
mano
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its a one to one mapping between a DataSource and a SessionFactory, so you will need three seperate configurations for your SessionFactory. So if you use hibernate.cfg.xml to do this you will need three versions, one for each DataSource.


or by getting 3 different data sources form app server using jndi.

Not sure I follow this part of your question. You can avoid this. If you have three different databases you will need three different DataSources in JNDI.
 
mano ranjan
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks for your suggestion.I will create 3 hibernate.cfg.xml as you said

thanks
mano
 
Brad Ko
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

If you want to manage transaction for all databases, can you do that with Hibernate?

For instance, I want to update data on both databases, but the first transaction on a DB has to be rollback after second transaction on the other DB fails. How can you handle this kind scenario?

Thanks in advance.

Brad
[ January 15, 2008: Message edited by: Brad Ko ]
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brad, can you repost this as a new thread. Thanks

Mark
 
Brad Ko
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've posted this topic Transaction management on multiple DB connections as new.

Cheers,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic