最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2023年09月11日 11:04:22   作者:codecraft  
這篇文章主要為大家介紹了TransactionSynchronization的invokeAfterCompletion事務(wù)源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

本文主要研究一下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é)束時(shí)候的狀態(tài),0表示事務(wù)已提交,1表示事務(wù)已回滾,2表示事務(wù)未知;與afterCommit的一個(gè)最重要的區(qū)別是afterCompletion的異常會(huì)被捕獲,不像afterCommit會(huì)拋給調(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方法會(huì)遍歷synchronizations,挨個(gè)執(zhí)行afterCompletion,注意這里catch了Throwable異常,進(jìn)行了error級(jí)別的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之類(lèi)的場(chǎng)景,它回傳的status是STATUS_UNKNOWN

小結(jié)

afterCompletion方法有入?yún)tatus,表示事務(wù)結(jié)束時(shí)候的狀態(tài),0表示事務(wù)已提交,1表示事務(wù)已回滾,2表示事務(wù)未知(一般是JTA相關(guān));與afterCommit的一個(gè)最重要的區(qū)別是afterCompletion的異常(Throwable)會(huì)被捕獲,不像afterCommit會(huì)拋給調(diào)用方

doc 聊聊spring的TransactionSynchronizationAdapter

以上就是TransactionSynchronization的invokeAfterCompletion事務(wù)源碼解析的詳細(xì)內(nèi)容,更多關(guān)于TransactionSynchronization invokeAfterCompletion的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Java+LibreOffice實(shí)現(xiàn)Excel轉(zhuǎn)PDF并橫向一頁(yè)顯示所有列

    Java+LibreOffice實(shí)現(xiàn)Excel轉(zhuǎn)PDF并橫向一頁(yè)顯示所有列

    在實(shí)際業(yè)務(wù)場(chǎng)景中,用戶(hù)往往會(huì)提供格式不一的 Excel 文件,有時(shí)希望將其轉(zhuǎn)換為 PDF 并橫向顯示,所有列壓縮在一頁(yè)內(nèi),下面我們來(lái)看看具體實(shí)現(xiàn)方法吧
    2025-06-06
  • springboot+mybatis如何屏蔽掉mybatis日志

    springboot+mybatis如何屏蔽掉mybatis日志

    這篇文章主要介紹了springboot+mybatis如何屏蔽掉mybatis日志問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • 從入門(mén)到精通詳解Java Apache POI操作Excel的實(shí)戰(zhàn)教程

    從入門(mén)到精通詳解Java Apache POI操作Excel的實(shí)戰(zhàn)教程

    在Java開(kāi)發(fā)中,Excel文件處理是一項(xiàng)常見(jiàn)且重要的技能,Apache POI作為Java操作Excel的經(jīng)典庫(kù),提供了強(qiáng)大的API支持,本文從基礎(chǔ)IO流操作Excel的局限性入手,本文詳細(xì)介紹了Apache POI的核心概念,依賴(lài)配置,以及實(shí)戰(zhàn)案例
    2026-05-05
  • Java面試題沖刺第十六天--消息隊(duì)列

    Java面試題沖刺第十六天--消息隊(duì)列

    這篇文章主要為大家分享了最有價(jià)值的三道關(guān)于消息隊(duì)列的面試題,涵蓋內(nèi)容全面,包括數(shù)據(jù)結(jié)構(gòu)和算法相關(guān)的題目、經(jīng)典面試編程題等,感興趣的小伙伴們可以參考一下
    2021-08-08
  • pagehelper分頁(yè)工具類(lèi)的封裝

    pagehelper分頁(yè)工具類(lèi)的封裝

    這篇文章主要為大家詳細(xì)介紹了pagehelper分頁(yè)工具類(lèi)的封裝,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • Spring JDBC的使用詳解

    Spring JDBC的使用詳解

    這篇文章主要介紹了Spring JDBC的使用詳解,幫助大家更好的理解和學(xué)習(xí)使用SpringBoot框架,感興趣的朋友可以了解下
    2021-05-05
  • Java中的值傳遞和引用傳遞區(qū)別解析

    Java中的值傳遞和引用傳遞區(qū)別解析

    這篇文章主要介紹了Java中的值傳遞和引用傳遞區(qū)別解析,引用類(lèi)型的變量保存引用值,“引用值”指向內(nèi)存空間的地址,代表了某個(gè)對(duì)象的引用,而不是對(duì)象本身,對(duì)象本身存放在該引用值所表示的地址的位置,包含:數(shù)組、類(lèi)、接口,需要的朋友可以參考下
    2023-11-11
  • Java接口回調(diào)的本質(zhì)詳解

    Java接口回調(diào)的本質(zhì)詳解

    大家好,本篇文章主要講的是Java接口回調(diào)的本質(zhì)詳解,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話(huà)記得收藏一下
    2022-02-02
  • Java中單例模式詳解

    Java中單例模式詳解

    這篇文章主要介紹了Java中單例模式詳解,單例模式包括了懶漢式單例、餓漢式單例、登記式單例三種,想要了解的朋友可以了解一下。
    2016-11-11
  • 關(guān)于spring依賴(lài)注入的方式以及優(yōu)缺點(diǎn)

    關(guān)于spring依賴(lài)注入的方式以及優(yōu)缺點(diǎn)

    這篇文章主要介紹了關(guān)于spring依賴(lài)注入的方式以及優(yōu)缺點(diǎn),依賴(lài)注入,是IOC的一個(gè)方面,是個(gè)通常的概念,它有多種解釋,這概念是說(shuō)你不用創(chuàng)建對(duì)象,而只需要描述它如何被創(chuàng)建,需要的朋友可以參考下
    2023-07-07

最新評(píng)論

蛟河市| 磐石市| 惠来县| 奉化市| 巧家县| 科技| 海宁市| 乐平市| 昌都县| 平罗县| 黄陵县| 玛曲县| 德昌县| 龙州县| 大渡口区| 宜兰县| 类乌齐县| 富宁县| 十堰市| 永济市| 织金县| 鄢陵县| 瑞昌市| 安国市| 临武县| 肇源县| 穆棱市| 鄯善县| 博爱县| 乌鲁木齐市| 桃江县| 北票市| 陇川县| 黔西| 普定县| 容城县| 汉沽区| 徐闻县| 连南| 舒兰市| 鸡西市|