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

spring事務(wù)的propagation傳播屬性示例詳解

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

本文主要研究一下spring事務(wù)的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事務(wù)定義了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方法對(duì)各個(gè)傳播級(jí)別進(jìn)行了處理,比如針對(duì)PROPAGATION_REQUIRES_NEW,它會(huì)執(zhí)行suspend當(dāng)前事務(wù),然后newTransactionStatus

小結(jié)

spring事務(wù)提供了各種傳播級(jí)別可以設(shè)置,它主要是為了方便處理事務(wù)嵌套的場(chǎng)景,可以支持里外共用一個(gè)事務(wù),或者里頭新開(kāi)事務(wù)等等,這樣子可以解決里外重復(fù)開(kāi)事務(wù)等問(wèn)題。但是這個(gè)用多了也要注意,在調(diào)用的時(shí)候需要設(shè)置什么傳播級(jí)別。其實(shí)其他語(yǔ)言可能沒(méi)有事務(wù)傳播這一說(shuō),一般就是盡量把代碼寫(xiě)簡(jiǎn)單點(diǎn),少用事務(wù)嵌套。

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

相關(guān)文章

  • Java中Double、Float類(lèi)型的NaN和Infinity的具體使用

    Java中Double、Float類(lèi)型的NaN和Infinity的具體使用

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

    對(duì)接支付寶java sdk文檔詳解

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

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

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

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

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

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

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

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

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

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

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

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

    建造者模式,是一種對(duì)象構(gòu)建模式 它可以將復(fù)雜對(duì)象的建造過(guò)程抽象出來(lái),使這個(gè)抽象過(guò)程的不同實(shí)現(xiàn)方法可以構(gòu)造出不同表現(xiàn)的對(duì)象。本文將通過(guò)示例講解建造者模式,需要的可以參考一下
    2023-02-02
  • Java實(shí)現(xiàn)的對(duì)稱(chēng)加密算法AES定義與用法詳解

    Java實(shí)現(xiàn)的對(duì)稱(chēng)加密算法AES定義與用法詳解

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

    Mybatis中Mapper映射文件使用詳解

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

最新評(píng)論

宜川县| 建瓯市| 宁晋县| 潮安县| 阳西县| 乐都县| 福州市| 江陵县| 墨玉县| 鹿邑县| 镶黄旗| 辽宁省| 邓州市| 大同县| 班玛县| 沙河市| 上犹县| 梁山县| 通州市| 色达县| 界首市| 天柱县| 沽源县| 富蕴县| 双辽市| 开化县| 承德县| 林口县| 聂拉木县| 吉木乃县| 潼关县| 金塔县| 洞头县| 平度市| 大兴区| 枝江市| 雅江县| 赣州市| 景洪市| 南阳市| 新野县|