本文介绍了仅标记为回滚的事务:如何查找原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  methodA(){
methodB()方法B在我的@Transactional方法中提交事务时遇到问题()
}

@Transactional
methodB(){
...
em.persist();
...
em.flush();
log(OK);
}

当我从methodA()调用methodB()时,我可以在日志中看到确定。但后来我得到了

 无法提交JPA事务;嵌套异常是javax.persistence.RollbackException:标记为rollbackOnly的事务org.springframework.transaction.TransactionSystemException:无法提交JPA事务;嵌套异常是javax.persistence.RollbackException:在org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:521)处标记为rollbackOnly 
的事务在org.springframework.transaction.support.AbstractPlatformTransactionManager处
.processCommit(AbstractPlatformTransactionManager.java:754)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport .java:393)
在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)上的

at org.springframework.aop.framework.Cglib2AopProxy $ DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:622)
at m ethodA()...




  1. methodB的上下文完全缺失例外情况 - 这是我想的吗?

  2. methodB()中的某些内容将事务标记为仅回滚?我怎样才能找到它?是否有例如一种方法来检查像 getCurrentTransaction()。isRollbackOnly()?之类的东西 - 像这样我可以遍历该方法并找到原因。 b $ b


解决方案

我终于明白了这个问题:

<$ p $ (){
methodB()


@Transactional(noRollbackFor = Exception.class)
methodB(){
...
try {
methodC()
} catch(...){...}
log(OK);
}

@Transactional
methodC(){
throw new ...();
}

即使 methodB code>具有正确的注释, methodC 不会。当抛出异常时,第二个 @Transactional 只会将第一个事务标记为Rollback。


I am having issues with committing a transaction within my @Transactional method:

methodA() {
    methodB()
}

@Transactional
methodB() {
    ...
    em.persist();
    ...
    em.flush();
    log("OK");
}

When I call methodB() from methodA(), the method passes successfuly and I can see "OK" in my logs. But then I get

Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly
    at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:521)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:393)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:622)
    at methodA()...
  1. The context of methodB is completely missing in the exception - which is okay I suppose?
  2. Something within the methodB() marked the transaction as rollback only? How can I find it out? Is there for instance a way to check something like getCurrentTransaction().isRollbackOnly()? - like this I could step through the method and find the cause.
解决方案

I finally understood the problem:

methodA() {
    methodB()
}

@Transactional(noRollbackFor = Exception.class)
methodB() {
    ...
    try {
        methodC()
    } catch (...) {...}
    log("OK");
}

@Transactional
methodC() {
    throw new ...();
}

What happens is that even though the methodB has the right annotation, the methodC does not. When the exception is thrown, the second @Transactional marks the first transaction as Rollback only anyway.

这篇关于仅标记为回滚的事务:如何查找原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 03:33