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

spring事務的propagation傳播屬性示例詳解

 更新時間:2023年09月14日 08:51:52   作者:codecraft  
這篇文章主要為大家介紹了spring事務的propagation傳播屬性示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

本文主要研究一下spring事務的propagation

Propagation

org/springframework/transaction/annotation/Propagation.java

/**
 * Enumeration that represents transaction propagation behaviors for use
 * with the {@link Transactional} annotation, corresponding to the
 * {@link TransactionDefinition} interface.
 *
 * @author Colin Sampaleanu
 * @author Juergen Hoeller
 * @since 1.2
 */
public enum Propagation {
    /**
     * Support a current transaction, create a new one if none exists.
     * Analogous to EJB transaction attribute of the same name.
     * <p>This is the default setting of a transaction annotation.
     */
    REQUIRED(TransactionDefinition.PROPAGATION_REQUIRED),
    /**
     * Support a current transaction, execute non-transactionally if none exists.
     * Analogous to EJB transaction attribute of the same name.
     * <p>Note: For transaction managers with transaction synchronization,
     * {@code SUPPORTS} is slightly different from no transaction at all,
     * as it defines a transaction scope that synchronization will apply for.
     * As a consequence, the same resources (JDBC Connection, Hibernate Session, etc)
     * will be shared for the entire specified scope. Note that this depends on
     * the actual synchronization configuration of the transaction manager.
     * @see org.springframework.transaction.support.AbstractPlatformTransactionManager#setTransactionSynchronization
     */
    SUPPORTS(TransactionDefinition.PROPAGATION_SUPPORTS),
    /**
     * Support a current transaction, throw an exception if none exists.
     * Analogous to EJB transaction attribute of the same name.
     */
    MANDATORY(TransactionDefinition.PROPAGATION_MANDATORY),
    /**
     * Create a new transaction, and suspend the current transaction if one exists.
     * Analogous to the EJB transaction attribute of the same name.
     * <p><b>NOTE:</b> Actual transaction suspension will not work out-of-the-box
     * on all transaction managers. This in particular applies to
     * {@link org.springframework.transaction.jta.JtaTransactionManager},
     * which requires the {@code javax.transaction.TransactionManager} to be
     * made available to it (which is server-specific in standard Java EE).
     * @see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager
     */
    REQUIRES_NEW(TransactionDefinition.PROPAGATION_REQUIRES_NEW),
    /**
     * Execute non-transactionally, suspend the current transaction if one exists.
     * Analogous to EJB transaction attribute of the same name.
     * <p><b>NOTE:</b> Actual transaction suspension will not work out-of-the-box
     * on all transaction managers. This in particular applies to
     * {@link org.springframework.transaction.jta.JtaTransactionManager},
     * which requires the {@code javax.transaction.TransactionManager} to be
     * made available to it (which is server-specific in standard Java EE).
     * @see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager
     */
    NOT_SUPPORTED(TransactionDefinition.PROPAGATION_NOT_SUPPORTED),
    /**
     * Execute non-transactionally, throw an exception if a transaction exists.
     * Analogous to EJB transaction attribute of the same name.
     */
    NEVER(TransactionDefinition.PROPAGATION_NEVER),
    /**
     * Execute within a nested transaction if a current transaction exists,
     * behave like {@code REQUIRED} otherwise. There is no analogous feature in EJB.
     * <p>Note: Actual creation of a nested transaction will only work on specific
     * transaction managers. Out of the box, this only applies to the JDBC
     * DataSourceTransactionManager. Some JTA providers might support nested
     * transactions as well.
     * @see org.springframework.jdbc.datasource.DataSourceTransactionManager
     */
    NESTED(TransactionDefinition.PROPAGATION_NESTED);
    private final int value;
    Propagation(int value) {
        this.value = value;
    }
    public int value() {
        return this.value;
    }
}
spring事務定義了Propagation枚舉,主要有REQUIRED、SUPPORTS、MANDATORY、REQUIRES_NEW、NOT_SUPPORTED、NEVER、NESTED

AbstractPlatformTransactionManager

org/springframework/transaction/support/AbstractPlatformTransactionManager.java

/**
     * Create a TransactionStatus for an existing transaction.
     */
    private TransactionStatus handleExistingTransaction(
            TransactionDefinition definition, Object transaction, boolean debugEnabled)
            throws TransactionException {
        if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NEVER) {
            throw new IllegalTransactionStateException(
                    "Existing transaction found for transaction marked with propagation 'never'");
        }
        if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NOT_SUPPORTED) {
            if (debugEnabled) {
                logger.debug("Suspending current transaction");
            }
            Object suspendedResources = suspend(transaction);
            boolean newSynchronization = (getTransactionSynchronization() == SYNCHRONIZATION_ALWAYS);
            return prepareTransactionStatus(
                    definition, null, false, newSynchronization, debugEnabled, suspendedResources);
        }
        if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRES_NEW) {
            if (debugEnabled) {
                logger.debug("Suspending current transaction, creating new transaction with name [" +
                        definition.getName() + "]");
            }
            SuspendedResourcesHolder suspendedResources = suspend(transaction);
            try {
                boolean newSynchronization = (getTransactionSynchronization() != SYNCHRONIZATION_NEVER);
                DefaultTransactionStatus status = newTransactionStatus(
                        definition, transaction, true, newSynchronization, debugEnabled, suspendedResources);
                doBegin(transaction, definition);
                prepareSynchronization(status, definition);
                return status;
            }
            catch (RuntimeException | Error beginEx) {
                resumeAfterBeginException(transaction, suspendedResources, beginEx);
                throw beginEx;
            }
        }
        if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NESTED) {
            if (!isNestedTransactionAllowed()) {
                throw new NestedTransactionNotSupportedException(
                        "Transaction manager does not allow nested transactions by default - " +
                        "specify 'nestedTransactionAllowed' property with value 'true'");
            }
            if (debugEnabled) {
                logger.debug("Creating nested transaction with name [" + definition.getName() + "]");
            }
            if (useSavepointForNestedTransaction()) {
                // Create savepoint within existing Spring-managed transaction,
                // through the SavepointManager API implemented by TransactionStatus.
                // Usually uses JDBC 3.0 savepoints. Never activates Spring synchronization.
                DefaultTransactionStatus status =
                        prepareTransactionStatus(definition, transaction, false, false, debugEnabled, null);
                status.createAndHoldSavepoint();
                return status;
            }
            else {
                // Nested transaction through nested begin and commit/rollback calls.
                // Usually only for JTA: Spring synchronization might get activated here
                // in case of a pre-existing JTA transaction.
                boolean newSynchronization = (getTransactionSynchronization() != SYNCHRONIZATION_NEVER);
                DefaultTransactionStatus status = newTransactionStatus(
                        definition, transaction, true, newSynchronization, debugEnabled, null);
                doBegin(transaction, definition);
                prepareSynchronization(status, definition);
                return status;
            }
        }
        // Assumably PROPAGATION_SUPPORTS or PROPAGATION_REQUIRED.
        if (debugEnabled) {
            logger.debug("Participating in existing transaction");
        }
        if (isValidateExistingTransaction()) {
            if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) {
                Integer currentIsolationLevel = TransactionSynchronizationManager.getCurrentTransactionIsolationLevel();
                if (currentIsolationLevel == null || currentIsolationLevel != definition.getIsolationLevel()) {
                    Constants isoConstants = DefaultTransactionDefinition.constants;
                    throw new IllegalTransactionStateException("Participating transaction with definition [" +
                            definition + "] specifies isolation level which is incompatible with existing transaction: " +
                            (currentIsolationLevel != null ?
                                    isoConstants.toCode(currentIsolationLevel, DefaultTransactionDefinition.PREFIX_ISOLATION) :
                                    "(unknown)"));
                }
            }
            if (!definition.isReadOnly()) {
                if (TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {
                    throw new IllegalTransactionStateException("Participating transaction with definition [" +
                            definition + "] is not marked as read-only but existing transaction is");
                }
            }
        }
        boolean newSynchronization = (getTransactionSynchronization() != SYNCHRONIZATION_NEVER);
        return prepareTransactionStatus(definition, transaction, false, newSynchronization, debugEnabled, null);
    }
AbstractPlatformTransactionManager的handleExistingTransaction方法對各個傳播級別進行了處理,比如針對PROPAGATION_REQUIRES_NEW,它會執(zhí)行suspend當前事務,然后newTransactionStatus

小結(jié)

spring事務提供了各種傳播級別可以設置,它主要是為了方便處理事務嵌套的場景,可以支持里外共用一個事務,或者里頭新開事務等等,這樣子可以解決里外重復開事務等問題。但是這個用多了也要注意,在調(diào)用的時候需要設置什么傳播級別。其實其他語言可能沒有事務傳播這一說,一般就是盡量把代碼寫簡單點,少用事務嵌套。

以上就是spring事務的propagation傳播屬性示例詳解的詳細內(nèi)容,更多關于spring事務propagation的資料請關注腳本之家其它相關文章!

相關文章

  • Java中Double、Float類型的NaN和Infinity的具體使用

    Java中Double、Float類型的NaN和Infinity的具體使用

    Java在處理浮點數(shù)運算時,提供了NaN和Infinity兩個常量,本文主要介紹了Java中Double、Float類型的NaN和Infinity的具體使用,文中通過示例代碼介紹的非常詳細,需要的朋友們下面隨著小編來一起學習學習吧
    2023-06-06
  • 對接支付寶java sdk文檔詳解

    對接支付寶java sdk文檔詳解

    文章概述支付寶H5、普通及掃碼支付服務的實現(xiàn),說明如何通過pom.xml管理依賴并配置Main入口類以集成支付功能
    2025-09-09
  • 教你使用idea搭建ssm詳細教程(Spring+Spring Mvc+Mybatis)

    教你使用idea搭建ssm詳細教程(Spring+Spring Mvc+Mybatis)

    今天教大家使用idea搭建ssm詳細教程(Spring+Spring Mvc+Mybatis),文中有非常詳細的圖文介紹及代碼示例,對正在學習使用idea的小伙伴很有幫助,需要的朋友可以參考下
    2021-05-05
  • SpringBoot配置文件高級用法實戰(zhàn)分享

    SpringBoot配置文件高級用法實戰(zhàn)分享

    Spring Boot配置文件的優(yōu)先級是一個重要的概念,它決定了當存在多個配置文件時,哪個配置文件中的配置將被優(yōu)先采用,本文給大家介紹了SpringBoot配置文件高級用法實戰(zhàn),文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2024-08-08
  • SpringBoot使用SOFA-Lookout監(jiān)控的方法

    SpringBoot使用SOFA-Lookout監(jiān)控的方法

    本文介紹SpringBoot使用螞蟻金服SOFA-Lookout配合Prometheus進行監(jiān)控,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03
  • Java中的Phaser并發(fā)階段器詳解

    Java中的Phaser并發(fā)階段器詳解

    這篇文章主要介紹了Java中的Phaser并發(fā)階段器詳解,Phaser由JDK1.7提出,是一個復雜強大的同步輔助類,是對同步工具類CountDownLatch和CyclicBarrier的綜合升級,能夠支持分階段實現(xiàn)等待的業(yè)務場景,需要的朋友可以參考下
    2023-12-12
  • Java數(shù)據(jù)結(jié)構之環(huán)形鏈表和約瑟夫問題詳解

    Java數(shù)據(jù)結(jié)構之環(huán)形鏈表和約瑟夫問題詳解

    約瑟夫(Josephus)問題是單向環(huán)形鏈表的一種體現(xiàn),也就是丟手帕問題,下面這篇文章主要給大家介紹了關于Java數(shù)據(jù)結(jié)構之環(huán)形鏈表和約瑟夫問題的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-08-08
  • Java創(chuàng)建型模式之建造者模式詳解

    Java創(chuàng)建型模式之建造者模式詳解

    建造者模式,是一種對象構建模式 它可以將復雜對象的建造過程抽象出來,使這個抽象過程的不同實現(xiàn)方法可以構造出不同表現(xiàn)的對象。本文將通過示例講解建造者模式,需要的可以參考一下
    2023-02-02
  • Java實現(xiàn)的對稱加密算法AES定義與用法詳解

    Java實現(xiàn)的對稱加密算法AES定義與用法詳解

    這篇文章主要介紹了Java實現(xiàn)的對稱加密算法AES,結(jié)合實例形式分析了對稱加密算法AES的定義、特點、用法及使用場景,需要的朋友可以參考下
    2018-04-04
  • Mybatis中Mapper映射文件使用詳解

    Mybatis中Mapper映射文件使用詳解

    這篇文章主要介紹了Mybatis中Mapper映射文件使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-06-06

最新評論

泸水县| 全南县| 盖州市| 贵阳市| 康马县| 翼城县| 石河子市| 泽普县| 翼城县| 湄潭县| 博野县| 松滋市| 合川市| 班玛县| 策勒县| 灵寿县| 肥乡县| 长治市| 南汇区| 青岛市| 贺州市| 江津市| 海阳市| 仲巴县| 林州市| 阳江市| 微山县| 安庆市| 和顺县| 乌鲁木齐县| 墨竹工卡县| 丹棱县| 宁陵县| 辽阳县| 洛川县| 汕尾市| 彰武县| 白沙| 兴业县| 海林市| 中西区|