Spring multiple transaction managers, single transaction

前端 未结 3 470
抹茶落季
抹茶落季 2020-12-30 12:34

I have a complex situation where I have to use 2 different databases, there for I use 2 different transaction managers. Is there a way in Spring to link these transaction ma

相关标签:
3条回答
  • 2020-12-30 13:04

    You need a global transaction manager which supports 2-phase-commit (XA). Several independent and free ones are available. I've used Bitronix in a Spring-based project, but there is also Atomikos, and probably others. See http://en.wikipedia.org/wiki/Java_Transaction_API#Opensource_JTA_implementations

    0 讨论(0)
  • 2020-12-30 13:08

    For routing through multiple datasource, You could use abstractRoutingDataSource but if you have requirements like one rollbackack affecting another you would need a JtaTransactionManager for distributed txn management .

    0 讨论(0)
  • 2020-12-30 13:28

    You can use Spring's JtaTransactionManager to make sure both DBs are transacted with a single transaction manager.

    Note, you would have to choose an underlying implementation which can either be a container's one: e.g. WebLogic, WebSphere and OC4J, etc.. or a stand alone, even an open source one: e.g. Atomikos.

    HOWEVER

    XA transaction management complicates things (configuration / performance / problem resolution / maintenance / etc.). And in a lot of cases, it can be avoided by clever patterns.

    To get a solid understanding on whether you need to use XA ( e.g. distributed ) transaction manager, take a look at this fantastic article by Spring's own Dave Syer: Distributed transactions in Spring, with and without XA

    0 讨论(0)
提交回复
热议问题