TransactionSynchronization的invokeAfterCompletion事務(wù)源碼解析
序
本文主要研究一下TransactionSynchronization的invokeAfterCompletion
afterCompletion
org/springframework/transaction/support/TransactionSynchronization.java
public interface TransactionSynchronization extends Flushable { /** Completion status in case of proper commit. */ int STATUS_COMMITTED = 0; /** Completion status in case of proper rollback. */ int STATUS_ROLLED_BACK = 1; /** Completion status in case of heuristic mixed completion or system errors. */ int STATUS_UNKNOWN = 2; //...... /** * Invoked after transaction commit. Can perform further operations right * <i>after</i> the main transaction has <i>successfully</i> committed. * <p>Can e.g. commit further operations that are supposed to follow on a successful * commit of the main transaction, like confirmation messages or emails. * <p><b>NOTE:</b> The transaction will have been committed already, but the * transactional resources might still be active and accessible. As a consequence, * any data access code triggered at this point will still "participate" in the * original transaction, allowing to perform some cleanup (with no commit following * anymore!), unless it explicitly declares that it needs to run in a separate * transaction. Hence: <b>Use {@code PROPAGATION_REQUIRES_NEW} for any * transactional operation that is called from here.</b> * @throws RuntimeException in case of errors; will be <b>propagated to the caller</b> * (note: do not throw TransactionException subclasses here!) */ default void afterCommit() { } /** * Invoked after transaction commit/rollback. * Can perform resource cleanup <i>after</i> transaction completion. * <p><b>NOTE:</b> The transaction will have been committed or rolled back already, * but the transactional resources might still be active and accessible. As a * consequence, any data access code triggered at this point will still "participate" * in the original transaction, allowing to perform some cleanup (with no commit * following anymore!), unless it explicitly declares that it needs to run in a * separate transaction. Hence: <b>Use {@code PROPAGATION_REQUIRES_NEW} * for any transactional operation that is called from here.</b> * @param status completion status according to the {@code STATUS_*} constants * @throws RuntimeException in case of errors; will be <b>logged but not propagated</b> * (note: do not throw TransactionException subclasses here!) * @see #STATUS_COMMITTED * @see #STATUS_ROLLED_BACK * @see #STATUS_UNKNOWN * @see #beforeCompletion */ default void afterCompletion(int status) { } }
afterCompletion方法有入?yún)tatus,表示事務(wù)結(jié)束時候的狀態(tài),0表示事務(wù)已提交,1表示事務(wù)已回滾,2表示事務(wù)未知;與afterCommit的一個最重要的區(qū)別是afterCompletion的異常會被捕獲,不像afterCommit會拋給調(diào)用方
invokeAfterCompletion
org/springframework/transaction/support/TransactionSynchronizationUtils.java
/** * Actually invoke the {@code afterCompletion} methods of the * given Spring TransactionSynchronization objects. * @param synchronizations a List of TransactionSynchronization objects * @param completionStatus the completion status according to the * constants in the TransactionSynchronization interface * @see TransactionSynchronization#afterCompletion(int) * @see TransactionSynchronization#STATUS_COMMITTED * @see TransactionSynchronization#STATUS_ROLLED_BACK * @see TransactionSynchronization#STATUS_UNKNOWN */ public static void invokeAfterCompletion(@Nullable List<TransactionSynchronization> synchronizations, int completionStatus) { if (synchronizations != null) { for (TransactionSynchronization synchronization : synchronizations) { try { synchronization.afterCompletion(completionStatus); } catch (Throwable tsex) { logger.error("TransactionSynchronization.afterCompletion threw exception", tsex); } } } }
可以看到TransactionSynchronizationUtils的invokeAfterCompletion方法會遍歷synchronizations,挨個執(zhí)行afterCompletion,注意這里catch了Throwable異常,進行了error級別的log
AbstractPlatformTransactionManager
org/springframework/transaction/support/AbstractPlatformTransactionManager.java
/** * Actually invoke the {@code afterCompletion} methods of the * given Spring TransactionSynchronization objects. * <p>To be called by this abstract manager itself, or by special implementations * of the {@code registerAfterCompletionWithExistingTransaction} callback. * @param synchronizations a List of TransactionSynchronization objects * @param completionStatus the completion status according to the * constants in the TransactionSynchronization interface * @see #registerAfterCompletionWithExistingTransaction(Object, java.util.List) * @see TransactionSynchronization#STATUS_COMMITTED * @see TransactionSynchronization#STATUS_ROLLED_BACK * @see TransactionSynchronization#STATUS_UNKNOWN */ protected final void invokeAfterCompletion(List<TransactionSynchronization> synchronizations, int completionStatus) { TransactionSynchronizationUtils.invokeAfterCompletion(synchronizations, completionStatus); } /** * Trigger {@code afterCompletion} callbacks. * @param status object representing the transaction * @param completionStatus completion status according to TransactionSynchronization constants */ private void triggerAfterCompletion(DefaultTransactionStatus status, int completionStatus) { if (status.isNewSynchronization()) { List<TransactionSynchronization> synchronizations = TransactionSynchronizationManager.getSynchronizations(); TransactionSynchronizationManager.clearSynchronization(); if (!status.hasTransaction() || status.isNewTransaction()) { if (status.isDebug()) { logger.trace("Triggering afterCompletion synchronization"); } // No transaction or new transaction for the current scope -> // invoke the afterCompletion callbacks immediately invokeAfterCompletion(synchronizations, completionStatus); } else if (!synchronizations.isEmpty()) { // Existing transaction that we participate in, controlled outside // of the scope of this Spring transaction manager -> try to register // an afterCompletion callback with the existing (JTA) transaction. registerAfterCompletionWithExistingTransaction(status.getTransaction(), synchronizations); } } }
AbstractPlatformTransactionManager的invokeAfterCompletion委托給了TransactionSynchronizationUtils.invokeAfterCompletion;triggerAfterCompletion主要是根據(jù)事務(wù)狀態(tài)執(zhí)行不同邏輯,分別是invokeAfterCompletion與registerAfterCompletionWithExistingTransaction,后者主要是JTA之類的場景,它回傳的status是STATUS_UNKNOWN
小結(jié)
afterCompletion方法有入?yún)tatus,表示事務(wù)結(jié)束時候的狀態(tài),0表示事務(wù)已提交,1表示事務(wù)已回滾,2表示事務(wù)未知(一般是JTA相關(guān)
);與afterCommit的一個最重要的區(qū)別是afterCompletion的異常(Throwable
)會被捕獲,不像afterCommit會拋給調(diào)用方
doc 聊聊spring的TransactionSynchronizationAdapter
以上就是TransactionSynchronization的invokeAfterCompletion事務(wù)源碼解析的詳細內(nèi)容,更多關(guān)于TransactionSynchronization invokeAfterCompletion的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java常用鎖synchronized和ReentrantLock的區(qū)別
這篇文章主要介紹了Java常用鎖synchronized和ReentrantLock的區(qū)別,二者的功效都是相同的,但又有很多不同點,下面我們就進入文章了解具體的相關(guān)內(nèi)容吧。需要的小伙伴也可以參考一下2022-05-05Java字符串駝峰與下?lián)Q線格式轉(zhuǎn)換如何實現(xiàn)
這篇文章主要介紹了Java字符串駝峰與下?lián)Q線格式轉(zhuǎn)換如何實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-11-11SpringBoot使用maven實現(xiàn)多環(huán)境運行和打包的操作步驟
在開發(fā)過程中,需要不斷進行環(huán)境的切換和打包部署,maven提供了多環(huán)境配置,可以方便實現(xiàn)不同環(huán)境的配置切換和打包,本文通過代碼示例給大家介紹的非常詳細,需要的朋友可以參考下2024-04-04java中構(gòu)造器內(nèi)部調(diào)用構(gòu)造器實例詳解
在本篇文章里小編給大家分享的是關(guān)于java中構(gòu)造器內(nèi)部調(diào)用構(gòu)造器實例內(nèi)容,需要的朋友們可以學(xué)習(xí)下。2020-05-05Spring Bean實例的創(chuàng)建及構(gòu)造器的挑選
這篇文章主要介紹了Spring Bean實例的創(chuàng)建及構(gòu)造器的挑選,文中有非常詳細的代碼示例,對正在學(xué)習(xí)java的小伙伴們有很好的幫助,需要的朋友可以參考下2021-04-04