File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Ramanuj Dev
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ramanuj Dev
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic