Spring5ソース読書ノート(4.1.3)トランザクションをコミットする(txInfo)commitTransactionAfterReturning

主な
トランザクションがバック圧延されたときの状態newTransactionの最初の裁判官は、newTransactionがtrueの場合、提出されました。

トランスミッションのモードがPROPAGATION_REQUIREDした場合:
内部外の出来事を使用するトランザクション、newTransactionの誤接続、トランザクションの内側の層は、提出時に提出されることはありませんトランザクションの外側の層によって、最終的な提出。

トランスミッションのモードがPROPAGATION_REQUIRES_NEWのとき:
内部トランザクションが新しい接続を作成するには、newTransactionは真、内部業務意志にトランザクションをコミットします。したがって、トランザクションのロールバックの後ろの内側層があっても変速機のこのモードは、トランザクション全体のロールバックを外層を生じ、内側層は、通信の内部業務の前にPROPAGATION_REQUIRES_NEWロールバックトランザクションの成功に提出されます。

ソースと
クラスTransactionAspectSupport

protected void commitTransactionAfterReturning(@Nullable TransactionInfo txInfo) {
	if (txInfo != null && txInfo.getTransactionStatus() != null) {
		if (logger.isTraceEnabled()) {
			logger.trace("Completing transaction for [" + txInfo.getJoinpointIdentification() + "]");
		}
		txInfo.getTransactionManager().commit(txInfo.getTransactionStatus());
	}
}

連絡先は、コミット:
クラスPlatformTransactionManager
ここに画像を挿入説明

@Override
public 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);
}

processCommitと:

private void processCommit(DefaultTransactionStatus status) throws TransactionException {
	try {
		boolean beforeCompletionInvoked = false;

		try {
			boolean unexpectedRollback = false;
			prepareForCommit(status);
			triggerBeforeCommit(status);
			triggerBeforeCompletion(status);
			beforeCompletionInvoked = true;

			//如果有回滚点
			if (status.hasSavepoint()) {
				if (status.isDebug()) {
					logger.debug("Releasing transaction savepoint");
				}
				unexpectedRollback = status.isGlobalRollbackOnly();
				//释放持有的回滚点
				status.releaseHeldSavepoint();
			}
			//如果是PROPAGATION_REQUIRED,最外层的才会走进来统一提交
			//如果是PROPAGATION_REQUIRES_NEW,每一个事务都会进来
			else if (status.isNewTransaction()) {
				if (status.isDebug()) {
					logger.debug("Initiating transaction commit");
				}
				unexpectedRollback = status.isGlobalRollbackOnly();
				doCommit(status);
			}
			else if (isFailEarlyOnGlobalRollbackOnly()) {
				unexpectedRollback = status.isGlobalRollbackOnly();
			}

			// Throw UnexpectedRollbackException if we have a global rollback-only
			// marker but still didn't get a corresponding exception from commit.
			if (unexpectedRollback) {
				throw new UnexpectedRollbackException(
						"Transaction silently rolled back because it has been marked as rollback-only");
			}
		}
		catch (UnexpectedRollbackException ex) {
			// can only be caused by doCommit
			triggerAfterCompletion(status, TransactionSynchronization.STATUS_ROLLED_BACK);
			throw ex;
		}
		catch (TransactionException ex) {
			// can only be caused by doCommit
			if (isRollbackOnCommitFailure()) {
				doRollbackOnCommitException(status, ex);
			}
			else {
				triggerAfterCompletion(status, TransactionSynchronization.STATUS_UNKNOWN);
			}
			throw ex;
		}
		catch (RuntimeException | Error ex) {
			if (!beforeCompletionInvoked) {
				triggerBeforeCompletion(status);
			}
			doRollbackOnCommitException(status, ex);
			throw ex;
		}

		// Trigger afterCommit callbacks, with an exception thrown there
		// propagated to callers but the transaction still considered as committed.
		try {
			triggerAfterCommit(status);
		}
		finally {
			triggerAfterCompletion(status, TransactionSynchronization.STATUS_COMMITTED);
		}

	}
	finally {
		cleanupAfterCompletion(status);
	}
}
公開された144元の記事 ウォンの賞賛250 ・は 10000 +を見て

おすすめ

転載: blog.csdn.net/weixin_44367006/article/details/104690196