欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

spring事務(wù)的REQUIRES_NEW源碼示例解析

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

本文主要研究一下spring事務(wù)的REQUIRES_NEW

TransactionDefinition

org/springframework/transaction/TransactionDefinition.java

/**
     * Create a new transaction, suspending 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 it to it (which is server-specific in standard Java EE).
     * <p>A {@code PROPAGATION_REQUIRES_NEW} scope always defines its own
     * transaction synchronizations. Existing synchronizations will be suspended
     * and resumed appropriately.
     * @see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager
     */
    int PROPAGATION_REQUIRES_NEW = 3;
PROPAGATION_REQUIRES_NEW在有事務(wù)的場(chǎng)景下會(huì)suspend當(dāng)前事務(wù),然后創(chuàng)建新事務(wù)

AbstractPlatformTransactionManager

org/springframework/transaction/support/AbstractPlatformTransactionManager.java

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;
            }
        }
handleExistingTransaction方法在判斷是PROPAGATION_REQUIRES_NEW,會(huì)執(zhí)行suspend方法,然后newTransactionStatus,執(zhí)行doBegin及prepareSynchronization

suspend

org/springframework/transaction/support/AbstractPlatformTransactionManager.java

/**
     * Suspend the given transaction. Suspends transaction synchronization first,
     * then delegates to the {@code doSuspend} template method.
     * @param transaction the current transaction object
     * (or {@code null} to just suspend active synchronizations, if any)
     * @return an object that holds suspended resources
     * (or {@code null} if neither transaction nor synchronization active)
     * @see #doSuspend
     * @see #resume
     */
    @Nullable
    protected final SuspendedResourcesHolder suspend(@Nullable Object transaction) throws TransactionException {
        if (TransactionSynchronizationManager.isSynchronizationActive()) {
            List<TransactionSynchronization> suspendedSynchronizations = doSuspendSynchronization();
            try {
                Object suspendedResources = null;
                if (transaction != null) {
                    suspendedResources = doSuspend(transaction);
                }
                String name = TransactionSynchronizationManager.getCurrentTransactionName();
                TransactionSynchronizationManager.setCurrentTransactionName(null);
                boolean readOnly = TransactionSynchronizationManager.isCurrentTransactionReadOnly();
                TransactionSynchronizationManager.setCurrentTransactionReadOnly(false);
                Integer isolationLevel = TransactionSynchronizationManager.getCurrentTransactionIsolationLevel();
                TransactionSynchronizationManager.setCurrentTransactionIsolationLevel(null);
                boolean wasActive = TransactionSynchronizationManager.isActualTransactionActive();
                TransactionSynchronizationManager.setActualTransactionActive(false);
                return new SuspendedResourcesHolder(
                        suspendedResources, suspendedSynchronizations, name, readOnly, isolationLevel, wasActive);
            }
            catch (RuntimeException | Error ex) {
                // doSuspend failed - original transaction is still active...
                doResumeSynchronization(suspendedSynchronizations);
                throw ex;
            }
        }
        else if (transaction != null) {
            // Transaction active but no synchronization active.
            Object suspendedResources = doSuspend(transaction);
            return new SuspendedResourcesHolder(suspendedResources);
        }
        else {
            // Neither transaction nor synchronization active.
            return null;
        }
    }
suspend方法主要是執(zhí)行doSuspendSynchronization方法返回suspendedSynchronizations,執(zhí)行doSuspend返回suspendedResources,最后根據(jù)這兩個(gè)創(chuàng)建SuspendedResourcesHolder

doSuspendSynchronization

/**
     * Suspend all current synchronizations and deactivate transaction
     * synchronization for the current thread.
     * @return the List of suspended TransactionSynchronization objects
     */
    private List<TransactionSynchronization> doSuspendSynchronization() {
        List<TransactionSynchronization> suspendedSynchronizations =
                TransactionSynchronizationManager.getSynchronizations();
        for (TransactionSynchronization synchronization : suspendedSynchronizations) {
            synchronization.suspend();
        }
        TransactionSynchronizationManager.clearSynchronization();
        return suspendedSynchronizations;
    }
doSuspendSynchronization這個(gè)遍歷suspendedSynchronizations,挨個(gè)執(zhí)行suspend,然后clearSynchronization

ResourceHolderSynchronization

org/springframework/transaction/support/ResourceHolderSynchronization.java

public void suspend() {
        if (this.holderActive) {
            TransactionSynchronizationManager.unbindResource(this.resourceKey);
        }
    }
ResourceHolderSynchronization的suspend執(zhí)行的是TransactionSynchronizationManager.unbindResource

unbindResource

org/springframework/transaction/support/TransactionSynchronizationManager.java

/**
     * Unbind a resource for the given key from the current thread.
     * @param key the key to unbind (usually the resource factory)
     * @return the previously bound value (usually the active resource object)
     * @throws IllegalStateException if there is no value bound to the thread
     * @see ResourceTransactionManager#getResourceFactory()
     */
    public static Object unbindResource(Object key) throws IllegalStateException {
        Object actualKey = TransactionSynchronizationUtils.unwrapResourceIfNecessary(key);
        Object value = doUnbindResource(actualKey);
        if (value == null) {
            throw new IllegalStateException(
                    "No value for key [" + actualKey + "] bound to thread [" + Thread.currentThread().getName() + "]");
        }
        return value;
    }
    /**
     * Actually remove the value of the resource that is bound for the given key.
     */
    @Nullable
    private static Object doUnbindResource(Object actualKey) {
        Map<Object, Object> map = resources.get();
        if (map == null) {
            return null;
        }
        Object value = map.remove(actualKey);
        // Remove entire ThreadLocal if empty...
        if (map.isEmpty()) {
            resources.remove();
        }
        // Transparently suppress a ResourceHolder that was marked as void...
        if (value instanceof ResourceHolder && ((ResourceHolder) value).isVoid()) {
            value = null;
        }
        if (value != null && logger.isTraceEnabled()) {
            logger.trace("Removed value [" + value + "] for key [" + actualKey + "] from thread [" +
                    Thread.currentThread().getName() + "]");
        }
        return value;
    }
unbindResource主要是執(zhí)行doUnbindResource,從resources中移除

cleanupAfterCompletion

org/springframework/transaction/support/AbstractPlatformTransactionManager.java

/**
     * Clean up after completion, clearing synchronization if necessary,
     * and invoking doCleanupAfterCompletion.
     * @param status object representing the transaction
     * @see #doCleanupAfterCompletion
     */
    private void cleanupAfterCompletion(DefaultTransactionStatus status) {
        status.setCompleted();
        if (status.isNewSynchronization()) {
            TransactionSynchronizationManager.clear();
        }
        if (status.isNewTransaction()) {
            doCleanupAfterCompletion(status.getTransaction());
        }
        if (status.getSuspendedResources() != null) {
            if (status.isDebug()) {
                logger.debug("Resuming suspended transaction after completion of inner transaction");
            }
            Object transaction = (status.hasTransaction() ? status.getTransaction() : null);
            resume(transaction, (SuspendedResourcesHolder) status.getSuspendedResources());
        }
    }
在內(nèi)嵌事務(wù)執(zhí)行完之后,會(huì)判斷是否有suspendedResources,如果有則執(zhí)行resume,恢復(fù)之前suspend的事務(wù)

resume

org/springframework/transaction/support/AbstractPlatformTransactionManager.java

/**
     * Resume the given transaction. Delegates to the {@code doResume}
     * template method first, then resuming transaction synchronization.
     * @param transaction the current transaction object
     * @param resourcesHolder the object that holds suspended resources,
     * as returned by {@code suspend} (or {@code null} to just
     * resume synchronizations, if any)
     * @see #doResume
     * @see #suspend
     */
    protected final void resume(@Nullable Object transaction, @Nullable SuspendedResourcesHolder resourcesHolder)
            throws TransactionException {
        if (resourcesHolder != null) {
            Object suspendedResources = resourcesHolder.suspendedResources;
            if (suspendedResources != null) {
                doResume(transaction, suspendedResources);
            }
            List<TransactionSynchronization> suspendedSynchronizations = resourcesHolder.suspendedSynchronizations;
            if (suspendedSynchronizations != null) {
                TransactionSynchronizationManager.setActualTransactionActive(resourcesHolder.wasActive);
                TransactionSynchronizationManager.setCurrentTransactionIsolationLevel(resourcesHolder.isolationLevel);
                TransactionSynchronizationManager.setCurrentTransactionReadOnly(resourcesHolder.readOnly);
                TransactionSynchronizationManager.setCurrentTransactionName(resourcesHolder.name);
                doResumeSynchronization(suspendedSynchronizations);
            }
        }
    }
resume方法執(zhí)行doResume,然后恢復(fù)之前的TransactionSynchronizationManager的一些設(shè)置

doResume

org/springframework/orm/jpa/JpaTransactionManager.java

@Override
    protected void doResume(@Nullable Object transaction, Object suspendedResources) {
        SuspendedResourcesHolder resourcesHolder = (SuspendedResourcesHolder) suspendedResources;
        TransactionSynchronizationManager.bindResource(
                obtainEntityManagerFactory(), resourcesHolder.getEntityManagerHolder());
        if (getDataSource() != null && resourcesHolder.getConnectionHolder() != null) {
            TransactionSynchronizationManager.bindResource(getDataSource(), resourcesHolder.getConnectionHolder());
        }
    }
doResume這里就是給bind回來

小結(jié)

spring事務(wù)的REQUIRES_NEW傳播級(jí)別的實(shí)現(xiàn)就是對(duì)當(dāng)前事務(wù)進(jìn)行suspend,底層是unbind,然后創(chuàng)建新事務(wù),執(zhí)行完畢判斷是否有suspend的事務(wù),有則執(zhí)行resume,底層是bind。具體對(duì)于mysql來講,它不感知這些嵌套事務(wù),它先接收到的是內(nèi)嵌的新事務(wù)的sql,然后提交,最后接收到了外層resume回來的事務(wù)。

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

相關(guān)文章

  • 構(gòu)建SpringBoot+MyBatis+Freemarker的項(xiàng)目詳解

    構(gòu)建SpringBoot+MyBatis+Freemarker的項(xiàng)目詳解

    在本篇內(nèi)容里小編給大家整理的是關(guān)于構(gòu)建SpringBoot+MyBatis+Freemarker的項(xiàng)目的具體步驟以及實(shí)例代碼,需要的朋友們參考下。
    2019-06-06
  • Java中閉包簡(jiǎn)單代碼示例

    Java中閉包簡(jiǎn)單代碼示例

    這篇文章主要介紹了Java中閉包簡(jiǎn)單代碼示例,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-01-01
  • 淺談Storm在zookeeper上的目錄結(jié)構(gòu)

    淺談Storm在zookeeper上的目錄結(jié)構(gòu)

    這篇文章主要介紹了淺談Storm在zookeeper上的目錄結(jié)構(gòu)的相關(guān)內(nèi)容,涉及storm使用zookeeper的操作以及詳細(xì)結(jié)構(gòu)圖,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-10-10
  • java使用nio2拷貝文件的示例

    java使用nio2拷貝文件的示例

    這篇文章主要介紹了java使用nio2拷貝文件的示例,需要的朋友可以參考下
    2014-04-04
  • Spring Boot和Hazelcast使用詳解

    Spring Boot和Hazelcast使用詳解

    這篇文章主要介紹了Spring Boot和Hazelcast使用詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-09-09
  • Spring?Boot?2.x升3.x的那些事

    Spring?Boot?2.x升3.x的那些事

    最近項(xiàng)目需求,準(zhǔn)備從Spring Boot 2.x升級(jí)到3.x,升級(jí)后發(fā)現(xiàn)編譯器報(bào)了一堆錯(cuò)誤,本文主要介紹了Spring?Boot?2.x升3.x的那些事,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-01-01
  • Java總結(jié)篇系列:Java泛型詳解

    Java總結(jié)篇系列:Java泛型詳解

    下面小編就為大家?guī)硪黄狫ava總結(jié)篇系列:Java泛型詳解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-09-09
  • spring系列筆記之常用注解

    spring系列筆記之常用注解

    這篇文章主要給大家介紹了關(guān)于spring系列筆記之常用注解的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用spring具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • 30分鐘入門Java8之方法引用學(xué)習(xí)

    30分鐘入門Java8之方法引用學(xué)習(xí)

    在Java8中,我們可以直接通過方法引用來簡(jiǎn)寫lambda表達(dá)式中已經(jīng)存在的方法,這篇文章主要介紹了30分鐘入門Java8之方法引用學(xué)習(xí),有興趣可以了解一下。
    2017-04-04
  • MyBatis-Plus雪花算法實(shí)現(xiàn)源碼解讀

    MyBatis-Plus雪花算法實(shí)現(xiàn)源碼解讀

    雪花算法是一種用于生成唯一標(biāo)識(shí)符(ID)的分布式算法,雪花算法的設(shè)計(jì)目標(biāo)是在分布式系統(tǒng)中生成全局唯一的ID,同時(shí)保證ID的有序性和趨勢(shì)遞增,這篇文章主要介紹了MyBatis-Plus雪花算法實(shí)現(xiàn)源碼解析,需要的朋友可以參考下
    2023-12-12

最新評(píng)論