gpt4 book ai didi

java - 事务标记为仅回滚 : How do I find the cause

转载 作者:IT老高 更新时间:2023-10-28 11:42:12 25 4
gpt4 key购买 nike

我在@Transactional 方法中提交事务时遇到问题:

methodA() {
methodB()
}

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

当我从 methodA() 调用 methodB() 时,该方法成功通过,我可以在日志中看到“OK”。但后来我得到了

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. 异常中完全缺少 methodB 的上下文 - 我想这可以吗?
  2. methodB() 中的某些东西将事务标记为仅回滚?我怎样才能找到它?例如有没有办法检查像 getCurrentTransaction().isRollbackOnly() 这样的东西? - 像这样我可以逐步检查该方法并找到原因。

最佳答案

当您将您的方法标记为 @Transactional 时,您的方法内发生的任何异常都会将周围的 TX 标记为仅回滚(即使您捕获它们)。您可以使用 @Transactional 注释的其他属性来防止它回滚,例如:

@Transactional(rollbackFor=MyException.class, noRollbackFor=MyException2.class)

关于java - 事务标记为仅回滚 : How do I find the cause,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19302196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com