[英]Nested transactions with different transaction managers in spring
I am trying to nest transactions with different transaction managers wherein if the nested transaction fails the outer main transaction needs to rollback as well我正在尝试使用不同的事务管理器嵌套事务,其中如果嵌套事务失败,外部主事务也需要回滚
@Transactional(transactionManager = "txManager1", propagation = Propagation.REQUIRED)
public int doOps() {
doSuccessfulDatabaseThings();
doOps2();
}
@Transactional(transactionManager = "txManager2", propagation = Propagation.REQUIRED)
public int doOps2() {
//this throws error
}
My spring config file has我的弹簧配置文件有
<bean id="dataSource1" class ="com.mchange.v2.c3p0.ComboPooledDataSource">
...
</bean>
<bean id="txManager1" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource1" />
</bean>
<!-- txManager2 defined similarly -->
<tx:annotation-driven/>
However, when doOps2()
fails, the transaction in doOps()
doesn't rollback.然而,当doOps2()
失败,在交易doOps()
不会回退。 How to make the rollback work?如何使回滚工作?
Bold Statement大胆的声明
You're doing it wrong.你这样做是错的。
Based on you description, you want a ChainedTransactionManager .根据您的描述,您需要一个ChainedTransactionManager 。
Create a transaction manager for each of your datasources, then pass the transaction manager to the ChainedTransactionManager
constructor.为每个数据源创建一个事务管理器,然后将事务管理器传递给ChainedTransactionManager
构造函数。 Name the ChainedTransactionManager
bean and reference the name in a @Transactional
annotation.命名ChainedTransactionManager
bean 并在@Transactional
注释中引用该名称。 I think the property is named "transactionManager".我认为该属性名为“transactionManager”。 For example,例如,
@Transactional(transactionManager = "chainedTransactionManagerBeanName")
When you are referencing the method inside the same bean, the @Transaction will be ignored.当您在同一个 bean 中引用该方法时,@Transaction 将被忽略。 These annotations are only used when referenced via Spring bean management, which will make a proxy:这些注解仅在通过 Spring bean 管理引用时使用,这将创建一个代理:
https://www.javacodegeeks.com/2016/05/understanding-transactional-annotation-spring.html#:~:text=At%20a%20high%20level%2C%20when,has%20no%20knowledge%20of%20it . https://www.javacodegeeks.com/2016/05/understanding-transactional-annotation-spring.html#:~:text=At%20a%20high%20level%2C%20when,has%20no%20knowledge%20of%20it .
So calling doOps2 doesnt do anything with the @Transactional txmanager2所以调用 doOps2 对@Transactional txmanager2 没有任何作用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.