4

I am using mulitple transaction managers (HibernateTransactionManager) one for each database.

During one code flow I believe I start/finish multiple transactions for each database.

I want to make sure all database transactions are part of single transaction. How can I acheive this?

One of the post here suggest to use JTATransaction manager, should I use that in combination with already using Hibernate?

Although I think In case of execption during any phase of application, all the transactions will be rolled back.

I am using Spring 4.1.* and hibernate 3.6.* and NOT using any webcontainer.

Community
  • 1
  • 1
Sankalp
  • 2,030
  • 7
  • 30
  • 41
  • NO they won't rollback. If you have 3 transactions, the first 2 commit, the last one fails, there is no way the 2 already committed transactions are going to rollback. – M. Deinum May 15 '15 at 07:26
  • It looks like you need XA transactions. http://www.javaworld.com/article/2077714/java-web-development/xa-transactions-using-spring.html – Howard Wang May 15 '15 at 07:49
  • @HowardWang I also found some suggestion for the same article so I asked whether need to use JTA. – Sankalp May 15 '15 at 07:55
  • @M.Deinum But as SpringFrameworkRef.statis "it marks a transaction for rollback in the case of runtime, unchecked exceptions" . So do you mean it will only rollback the current transaction whose method threw exception? SpringFramework reference here: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/transaction.html#transaction-declarative-rolling-back – Sankalp May 15 '15 at 08:07
  • But that is only for the current transactions and the transactions that haven't already commit. Imagine tx a, b, c. C already committed, B fails, A and B rollback but there is noway C is going to rollback as that is already committed. It can be even worse if you have a catch in between then only B could be rolled back and A and C would/could still commit. – M. Deinum May 15 '15 at 08:38
  • Are you saving different data to different databases or are you trying to implement some kind of replication/sharding? – breakline May 15 '15 at 09:00
  • @user2710256 They perform different business and database, so No sharding/replication. If that was required then I would have used some readymade solution for that purpose. – Sankalp May 15 '15 at 09:28
  • Check out this article, it might help you: http://fabiomaffioletti.me/blog/2014/04/15/distributed-transactions-multiple-databases-spring-boot-spring-data-jpa-atomikos/ – breakline May 15 '15 at 09:33

1 Answers1

1

It is years since I worked with Hibernate, so apologies in advance if any of this is misleading.

  • You need JTA transactions. Your DataSources need to support JTA.
  • If you are running in a web container like Weblogic or Websphere, then creating a JTA transaction is easy. I don't think Tomcat will suffice here. From a command line program, I have not found an answer.
  • If you have full control over the three databases, you should be fine. But be aware that rollback over systems that involve web services can be patchy or require non-standard protocols (e.g. Weblogic only supports SOAP JTA over t3 protocol)

If you get results, please share as the amount of good advice in this area is minimal.

(edit)Just noticed you clearly stated that you are not using a web container. If you come across a solution for a command line program, there are lots of us that would like to know. Building JUnit tests for distributed transactions is something I want to do, but have never found how to generate the JTA transaction object (/edit>)

kiwiron
  • 1,677
  • 11
  • 17