Spring 多事务管理器,单事务 [英] Spring multiple transaction managers, single transaction

查看:33
本文介绍了Spring 多事务管理器,单事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个复杂的情况,我必须使用 2 个不同的数据库,因为我使用了 2 个不同的事务管理器.Spring 有没有办法将这些事务管理器链接到单个事务中?如果第二个数据源发生异常,则应回滚第一个数据源的更改.

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 managers to work in a single transaction ? In case of an exception on the second dataSource changes on the first should be rolled-back.

<bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract="true">
 <property name="transactionManager" ref="transactionManager" />
 <property name="transactionAttributes">
  <props>
    <prop key="insert*">PROPAGATION_REQUIRED</prop>
    <prop key="update*">PROPAGATION_REQUIRED</prop>
    <prop key="save*">PROPAGATION_REQUIRED</prop>
    <prop key="remove*">PROPAGATION_REQUIRED</prop>
    <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
  </props>
 </property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
 <property name="driverClassName" value="oracle.jdbc.OracleDriver" />
 <property name="url" value="jdbc:oracle:thin:@dummyHost:1521:dummySID" />
 <property name="username" value="owner" />
 <property name="password" value="password" />
</bean>

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
 <property name="dataSource" ref="dataSource" />
</bean>

<bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
 <property name="driverClassName" value="oracle.jdbc.OracleDriver" />
 <property name="url" value="jdbc:oracle:thin:@dummyHost2:1521:dummySID2" />
 <property name="username" value="owner" />
 <property name="password" value="password" />
</bean>

<bean id="transactionManager2" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
 <property name="dataSource" ref="dataSource2" />
</bean>

推荐答案

你可以使用 Spring 的 JtaTransactionManager 以确保两个数据库都使用单个事务管理器进行交易.

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

请注意,您必须选择一个底层实现,它可以是容器的实现:例如WebLogic、WebSphere 和 OC4J 等.或者是独立的,甚至是开源的:例如Atomikos.

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.

XA 事务管理使事情复杂化(配置/性能/问题解决/维护等).在很多情况下,它可以通过巧妙的模式避免.

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

要深入了解是否需要使用 XA(例如分布式)事务管理器,请查看 Spring 自己的 Dave Syer 撰写的这篇精彩文章:Spring 中的分布式事务,有和没有 XA

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

这篇关于Spring 多事务管理器,单事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆