使用子事务(saveponit)时,当子事务抛出异常后,此异常处理掉不继续往外抛, Transaction has been rolled back because it has been marke

场景:在Service类中使用子事务(saveponit)时,当子事务抛出异常后,此异常处理掉不继续往外抛,spring在提交主事务时会抛出

org.springframework.transaction.UnexpectedRollbackException: Transaction has been rolled back because it has been marked as rollback

 认真的您可能会发现,在 org.springframework.transaction.support.AbstractPlatformTransactionManager 中有个叫

isGlobalRollbackOnParticipationFailure的参数,默认是true.

源码中说

Switch this to "false" to let the transaction originator make the rollback decision. If a participating transaction fails with an exception, the caller can still decide to continue with a different path within the transaction. However, note that this will only work as long as all participating resources are capable of continuing towards a transaction commit even after a data access failure: This is generally not the case for a Hibernate Session, for example; neither is it for a sequence of JDBC insert/update/delete operations.

          大意是:如果isGlobalRollbackOnParticipationFailure为false,则会让主事务决定回滚,如果当遇到exception加入事务失败时,调用者能继续在事务内决定是回滚还是继续。然而,要注意是那样做仅仅适用于在数据访问失败的情况下且只要所有操作事务能提交。

初步解决方案:

<bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /><property name="globalRollbackOnParticipationFailure" value="false" /> <!--指定此参数为false-->

</bean>


以上是在另一篇博客中看见的关于 Transaction has been rolled back because it has been marked as rollback

的解析和解决方案。本人测试过。解析和解决是正确的,但是还是没有解决我的问题,我是在事务处理的时候发现这个问题,如果改为flase后,那么你的事务执行顺序就要仔细排序,并且在可能出现异常的地方一定要加入异常捕获,不然就失去了事务配置的意义

比如A事件是正确,B事件是错误的,执行顺序是AB,那么FLASE这样配置之后,A还是正确执行了,B没有,即使加入了异常捕获。事务的配置失去了意义。

事件执行顺序BA,这样B执行发生异常的时候,如果没有及时捕获并处理,那么A还是会被执行,事务还是失去了意义。

················································

mybitas+spring.开发 声明拦截配置事务,遇见了一个问题,事务和异常同时发生时,捕获到了异常却没有正确回滚。

网上有很多说法,都是解决一个没有解决第二个,还在求解中,写出来希望大家看到的一起交流。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值