当前位置: 代码迷 >> 综合 >> Spring事务 Transaction rolled back because it has been marked as rollback-only
  详细解决方案

Spring事务 Transaction rolled back because it has been marked as rollback-only

热度:45   发布时间:2024-01-31 01:21:25.0

前言

  • 使用Spring事务时,出现异常:org.springframework.transaction.UnexpectedRollbackException: Transaction rolled back because it has been marked as rollback-only
  • 这个错误正确的理解应为:在提交事务时,发现该事务已经回滚了!!!
  • 通常该错误发生在事务嵌套时,异常被中途捕获,导致上层的事务提交时失败。

部分错误日志

19:29:37.202 [http-nio-80-exec-11] ERROR c.o.f.w.e.GlobalExceptionHandler - [notFount,64] - 运行时异常:
org.springframework.transaction.UnexpectedRollbackException: Transaction rolled back because it has been marked as rollback-onlyat org.springframework.transaction.support.AbstractPlatformTransactionManager.processRollback(AbstractPlatformTransactionManager.java:873)at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:710)at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:533)at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:304)at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)

分析代码

异常抛出的代码片段(org.springframework.transaction.support.AbstractPlatformTransactionManager.processRollback):

	/*** Process an actual rollback.* The completed flag has already been checked.* @param status object representing the transaction* @throws TransactionException in case of rollback failure*/private void processRollback(DefaultTransactionStatus status, boolean unexpected) {try {boolean unexpectedRollback = unexpected;try {triggerBeforeCompletion(status);if (status.hasSavepoint()) {if (status.isDebug()) {logger.debug("Rolling back transaction to savepoint");}status.rollbackToHeldSavepoint();}else if (status.isNewTransaction()) {if (status.isDebug()) {logger.debug("Initiating transaction rollback");}doRollback(status);}else {// Participating in larger transactionif (status.hasTransaction()) {if (status.isLocalRollbackOnly() || isGlobalRollbackOnParticipationFailure()) {if (status.isDebug()) {logger.debug("Participating transaction failed - marking existing transaction as rollback-only");}doSetRollbackOnly(status);}else {if (status.isDebug()) {logger.debug("Participating transaction failed - letting transaction originator decide on rollback");}}}else {logger.debug("Should roll back transaction but cannot - no transaction available");}// Unexpected rollback only matters here if we're asked to fail earlyif (!isFailEarlyOnGlobalRollbackOnly()) {unexpectedRollback = false;}}}catch (RuntimeException | Error ex) {triggerAfterCompletion(status, TransactionSynchronization.STATUS_UNKNOWN);throw ex;}triggerAfterCompletion(status, TransactionSynchronization.STATUS_ROLLED_BACK);// Raise UnexpectedRollbackException if we had a global rollback-only markerif (unexpectedRollback) {throw new UnexpectedRollbackException("Transaction rolled back because it has been marked as rollback-only");}}finally {cleanupAfterCompletion(status);}}

通过这段代码,可以看出,出现了非预期的回滚(unexpectedRollback)。

似懂非懂了。

再看看调用processRollback方法的org.springframework.transaction.support.AbstractPlatformTransactionManager.commit方法中的代码片段:

	/*** This implementation of commit handles participating in existing* transactions and programmatic rollback requests.* Delegates to {@code isRollbackOnly}, {@code doCommit}* and {@code rollback}.* @see org.springframework.transaction.TransactionStatus#isRollbackOnly()* @see #doCommit* @see #rollback*/@Overridepublic final void commit(TransactionStatus status) throws TransactionException {if (status.isCompleted()) {throw new IllegalTransactionStateException("Transaction is already completed - do not call commit or rollback more than once per transaction");}DefaultTransactionStatus defStatus = (DefaultTransactionStatus) status;if (defStatus.isLocalRollbackOnly()) {if (defStatus.isDebug()) {logger.debug("Transactional code has requested rollback");}processRollback(defStatus, false);return;}if (!shouldCommitOnGlobalRollbackOnly() && defStatus.isGlobalRollbackOnly()) {if (defStatus.isDebug()) {logger.debug("Global transaction is marked as rollback-only but transactional code requested commit");}processRollback(defStatus, true);return;}processCommit(defStatus);}

这段代码的中心思想,是要提交事务。但,阴差阳错的没有提交事务。
调用processRollback前有这样一句话:Global transaction is marked as rollback-only but transactional code requested commit(全局事务标记为仅回滚,但事务代码请求提交)。

结合上下文理解为:在提交事务时,发现该事务已经回滚了。所以,必须抛出错误。

  相关解决方案