亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

TransactionSynchronization的invokeAfterCompletion事務(wù)源碼解析

 更新時間:2023年09月11日 11:04:22   作者:codecraft  
這篇文章主要為大家介紹了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ū)別

    這篇文章主要介紹了Java常用鎖synchronized和ReentrantLock的區(qū)別,二者的功效都是相同的,但又有很多不同點,下面我們就進入文章了解具體的相關(guān)內(nèi)容吧。需要的小伙伴也可以參考一下
    2022-05-05
  • Java Swing JLabel標簽的使用方法

    Java Swing JLabel標簽的使用方法

    這篇文章主要介紹了Java Swing JLabel標簽的使用方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • Java字符串駝峰與下?lián)Q線格式轉(zhuǎn)換如何實現(xiàn)

    Java字符串駝峰與下?lián)Q線格式轉(zhuǎn)換如何實現(xiàn)

    這篇文章主要介紹了Java字符串駝峰與下?lián)Q線格式轉(zhuǎn)換如何實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-11-11
  • SpringBoot使用maven實現(xiàn)多環(huán)境運行和打包的操作步驟

    SpringBoot使用maven實現(xiàn)多環(huán)境運行和打包的操作步驟

    在開發(fā)過程中,需要不斷進行環(huán)境的切換和打包部署,maven提供了多環(huán)境配置,可以方便實現(xiàn)不同環(huán)境的配置切換和打包,本文通過代碼示例給大家介紹的非常詳細,需要的朋友可以參考下
    2024-04-04
  • java如何獲取10位和13位時間戳

    java如何獲取10位和13位時間戳

    這篇文章主要介紹了java如何獲取10位和13位時間戳問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • java中構(gòu)造器內(nèi)部調(diào)用構(gòu)造器實例詳解

    java中構(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-05
  • 使用spring容器在初始化Bean時前和后的操作

    使用spring容器在初始化Bean時前和后的操作

    這篇文章主要介紹了使用spring容器在初始化Bean時前和后的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Spring Bean實例的創(chuàng)建及構(gòu)造器的挑選

    Spring Bean實例的創(chuàng)建及構(gòu)造器的挑選

    這篇文章主要介紹了Spring Bean實例的創(chuàng)建及構(gòu)造器的挑選,文中有非常詳細的代碼示例,對正在學(xué)習(xí)java的小伙伴們有很好的幫助,需要的朋友可以參考下
    2021-04-04
  • 使用MDC實現(xiàn)日志鏈路跟蹤

    使用MDC實現(xiàn)日志鏈路跟蹤

    這篇文章主要介紹了使用MDC實現(xiàn)日志鏈路跟蹤,在微服務(wù)環(huán)境中,我們經(jīng)常使用Skywalking、CAT等去實現(xiàn)整體請求鏈路的追蹤,但是這個整體運維成本高,架構(gòu)復(fù)雜,我們來使用MDC通過Log來實現(xiàn)一個輕量級的會話事務(wù)跟蹤功能,下面就來看看具體的過程吧,需要的朋友可以參考一下
    2022-01-01
  • Java從零開始創(chuàng)建功能豐富的PDF文檔

    Java從零開始創(chuàng)建功能豐富的PDF文檔

    隨著數(shù)字化辦公的普及,PDF格式因其跨平臺兼容性和安全性而被廣泛應(yīng)用于各種場景中,本文將詳細介紹如何使用Java來實現(xiàn)PDF文檔的一鍵生成,有需要的可以了解下
    2025-01-01

最新評論