0
在我的项目中,我需要在一个事务中处理更多的数据库。交易注释或xml
1:使用注释,此报告错误 “重复标注”
public class TransactionalService {
@Transactional("order")
@Transactional("account")
public void processTwoDatabases(String name) { ... }
}
XML段跟随
<bean id="transactionManager1"
class="org.springframework.jdbc.DataSourceTransactionManager">
<qualifier value="order"/>
</bean>
<bean id="transactionManager2"
class="org.springframework.jdbc.DataSourceTransactionManager">
<qualifier value="account"/>
</bean>
2:但是,使用XML,它工作正常:
<tx:advice id="txAdvice1" transaction-manager="transactionManager1">
<!-- 定义方法的过滤规则 -->
<tx:attributes>
<tx:method name="process*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception"/>
</tx:attributes>
</tx:advice>
<aop:config proxy-target-class="true">
<aop:pointcut expression="execution (* com.service.impl.*.*(..))" id="services1"/>
<aop:advisor advice-ref="txAdvice1" pointcut-ref="services1"/>
</aop:config>
<tx:advice id="txAdvice2" transaction-manager="transactionManager2">
<tx:attributes>
<tx:method name="process*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception"/>
</tx:attributes>
</tx:advice>
<aop:config proxy-target-class="true">
<aop:pointcut expression="execution (* com.service.impl.*.*(..))" id="services2"/>
<aop:advisor advice-ref="txAdvice2" pointcut-ref="services2"/>
</aop:config>
我要回滚所有交易,如果这两个交易中的任何一个失败。在我的方式,两个transations如下:
'(1)为Transaction1开始' '(2)transaction2开始' '(3)我的业务逻辑代码' '(4)交易2端' '(5 )transaction1结束' 所以在1-4之间引发任何异常,两个事务将回滚。唯一的飞行是(4)和(5)之间抛出的异常,只有外部事务将回滚 ,但内部事务将提交.'@Roman Puchkovskiy' –
请注意我关于XT Transaction的编辑 –