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 DB connections using hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "multiple DB connections using hibernate" Watch "multiple DB connections using hibernate" New topic
Author

multiple DB connections using hibernate

mano ranjan
Ranch Hand

Joined: Jul 12, 2007
Posts: 102
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

Joined: Apr 14, 2004
Posts: 10336

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)


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
mano ranjan
Ranch Hand

Joined: Jul 12, 2007
Posts: 102
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

Joined: Apr 14, 2004
Posts: 10336

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

Joined: Jul 12, 2007
Posts: 102
Hi,
Thanks for your suggestion.I will create 3 hibernate.cfg.xml as you said

thanks
mano
Brad Ko
Greenhorn

Joined: Jul 27, 2007
Posts: 10
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

Joined: Feb 05, 2001
Posts: 17249
    
    6

Brad, can you repost this as a new thread. Thanks

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Brad Ko
Greenhorn

Joined: Jul 27, 2007
Posts: 10
I've posted this topic Transaction management on multiple DB connections as new.

Cheers,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: multiple DB connections using hibernate
 
Similar Threads
Hibernate with Application server connection pool
Hibernate with DAO?
Adding items from jsp to a list in java
MYSQL 5.0 Connection Time Out
Transaction management on multiple DB connections using hibernate