spring的事务中程序控制事务成功失败(Transaction marked as rollback)

A方法之外加有事务管理拦截器,在A方法中做一系列操作,操作过程中捕获了一个异常,因为此异常不影响业务,捕获后需要正常向下运行,最终事务管理器提交事务时报了如下错误
Transaction has been rolled back because it has been marked as rollback 
原因就是发生异常后当前的事务就被标记为rollback-only,外层事务管理器再commit时就会抛此异常,解决方法是
<bean id="transactionManager"
  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory">
   <ref bean="sessionFactory" />
  </property>
  <property name="globalRollbackOnParticipationFailure" value="false" />
 </bean>

在 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加入事务失败时,调用者能继续在事务内决定是回滚还是继续。然而,要注意是那样做仅仅适用于在数据访问失败的情况下且只要所有操作事务能提交。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值