/** * 提交的这个实现处理参与现有事务和编程式回滚请求。 * 委托给 {@code isRollbackOnly}, {@code doCommit} and {@code rollback}. * * @see TransactionStatus#isRollbackOnly() * @see #doCommit * @see #rollback */ @Override publicfinalvoidcommit(TransactionStatus status)throws TransactionException { //事务已经完成—不要在每个事务中调用commit或rollback超过一次 if (status.isCompleted()) { thrownewIllegalTransactionStateException( "Transaction is already completed - do not call commit or rollback more than once per transaction"); }
DefaultTransactionStatusdefStatus= (DefaultTransactionStatus) status; //通过检查这个事务状态确定只回滚标志。默认时false, 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; }
// Throw UnexpectedRollbackException if we have a global rollback-only // marker but still didn't get a corresponding exception from commit. if (unexpectedRollback) { //事务悄悄地回滚,因为它被标记为仅回滚 thrownewUnexpectedRollbackException( "Transaction silently rolled back because it has been marked as rollback-only"); } } catch (UnexpectedRollbackException ex) { // can only be caused by doCommit // 触发 afterCompletion回调。 triggerAfterCompletion(status, TransactionSynchronization.STATUS_ROLLED_BACK); throw ex; } catch (TransactionException ex) { // can only be caused by doCommit if (isRollbackOnCommitFailure()) { //调用{@code doRollback},正确处理回滚异常。 doRollbackOnCommitException(status, ex); } else { //触发{@code afterCompletion}回调。 triggerAfterCompletion(status, TransactionSynchronization.STATUS_UNKNOWN); } throw ex; } catch (RuntimeException | Error ex) { if (!beforeCompletionInvoked) { //触发{@code beforeCompletion}回调。 triggerBeforeCompletion(status); } //调用{@code doRollback},正确处理回滚异常。 doRollbackOnCommitException(status, ex); throw ex; }
// Trigger afterCommit callbacks, with an exception thrown there // propagated to callers but the transaction still considered as committed. try { //触发{@code afterCommit}回调。 triggerAfterCommit(status); } finally { //触发{@code afterCompletion}回调。 triggerAfterCompletion(status, TransactionSynchronization.STATUS_COMMITTED); }
/** * This implementation of rollback handles participating in existing * transactions. Delegates to {@code doRollback} and * {@code doSetRollbackOnly}. * * @see #doRollback * @see #doSetRollbackOnly */ @Override publicfinalvoidrollback(TransactionStatus status)throws TransactionException { if (status.isCompleted()) { thrownewIllegalTransactionStateException( "Transaction is already completed - do not call commit or rollback more than once per transaction"); }
// Raise UnexpectedRollbackException if we had a global rollback-only marker if (unexpectedRollback) { //事务回滚,因为它被标记为仅回滚 thrownewUnexpectedRollbackException( "Transaction rolled back because it has been marked as rollback-only"); } } finally { //完成后进行清理,必要时清除同步,并在完成后调用doCleanupAfterCompletion。 cleanupAfterCompletion(status); } }