This week's book giveaway is in the NodeJS forum.
We're giving away four copies of Serverless Applications with Node.js and have Slobodan Stojanovic & Aleksandar Simovic on-line!
See this thread for details.
Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

multiple DB connections using hibernate  RSS feed

 
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
 
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
 
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 ]
 
ranger
Posts: 17344
11
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,
 
Surfs up space ponies, I'm making gravy without this lumpy, tiny ad:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!