JPA出现Transaction marked as rollback only异常 总结
Posted 阿征new
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JPA出现Transaction marked as rollback only异常 总结相关的知识,希望对你有一定的参考价值。
总结:一个事务方法A调用另一个事务方法B时,B如果报错事务就已经是回滚状态了,放回到A之后,A方法继续执行,提交了事务(已经是回滚状态的事务),就报错了 Transaction marked as rollback only。
参考如下:
JPA出现Transaction marked as rollback only异常http://www.mzone.cc/article/571.html
摘抄:应该是事务被提交了两次导致的错误。再仔细查看代码发现是在service中嵌套调用了另外一个service的更新操作,从而导致了事务被两次提交。spring在处理事务时,多个service嵌套调用时使用的都是同一个事务,而不是每个不同的方法都使用新事务
spring的事务中程序控制事务成功失败(Transaction marked as rollback)http://jsczxy2.iteye.com/blog/1773795
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加入事务失败时,调用者能继续在事务内决定是回滚还是继续。然而,要注意是那样做仅仅适用于在数据访问失败的情况下且只要所有操作事务能提交。
以上是关于JPA出现Transaction marked as rollback only异常 总结的主要内容,如果未能解决你的问题,请参考以下文章
Transaction rolled back because it has been marked as rollback-only
没有 JPA @Transaction 和 save() 啥时候提交完成?
org.springframework.transaction.CannotCreateTransactionException:无法为事务打开 JPA EntityManager
调用super的方法时出现JPA Transaction错误
Transaction rolled back because it has been marked as rollback-only