File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes How to save data in 2 db simultaneously by using Spring Hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "How to save data in 2 db simultaneously by using Spring Hibernate" Watch "How to save data in 2 db simultaneously by using Spring Hibernate" New topic
Author

How to save data in 2 db simultaneously by using Spring Hibernate

Ramanuj Dev
Greenhorn

Joined: Jul 30, 2009
Posts: 5
Hi all,

how can I save data in 2 different database like Oracle and Mysql simultaneously. I am using Spring 2.5 and Hibernate for my project.

Please help on this.

Thanks in advance
Ram
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Ramanuj Dev wrote:Hi all,

how can I save data in 2 different database like Oracle and Mysql simultaneously. I am using Spring 2.5 and Hibernate for my project.

Please help on this.

Thanks in advance
Ram


You will have to have two different dataSources. One for each database, and with Hibernate you will need two SessionFactory(s) one for each database, or 2 EntityManagerFactory(s) if you are using Hibernate just as your implementation of JPA. And unless you use JTA, then you will also need two TransactionManagers.

This will be true whether you are using Spring or not. It is about the database stuff.

Mark


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

Joined: Jul 30, 2009
Posts: 5
Hi Mark,

Thanks for the response.
Now I am able to save data in 2 database(Oracle and MySQL) simultaneously.
I have another concerned here.
If I have only one database connection active and another database is stopped/disconnected, then I am getting error.
What i will set in my config file to fix this issue.
How I will select the active database through my config file???

Please help me on this.

Thanks in advance

Ram

Andrew Workman
Greenhorn

Joined: Oct 23, 2012
Posts: 1
Hi Ram,
which behaviour are you expecting in case of nonactive database?
If you want to rollback the whole transaction, do something like this:

If you want to save data anyway in your active database, try this:


But remember that you will have to configure distrubuted transaction support in first case.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

But remember that you will have to configure distrubuted transaction support in first case.


Be careful. While this is important, it adds confusion to the original poster.

So I am confused, if you want to save to one but not the other if it is down. Are you trying to do this as a failover or backup design? If not, then I would probably think that you wouldn't want a partial commit, you would want it to fail if one of them is down.

Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to save data in 2 db simultaneously by using Spring Hibernate