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

Spring事務(wù)框架之TransactionStatus源碼解析

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

Spring事務(wù)框架

本文來分析TransactionStatus。

用來記錄事務(wù)執(zhí)行過程中的狀態(tài)的,最終決定該事務(wù)能否提交、是否需要回滾等。

先來看一下TransactionStatus的類結(jié)構(gòu):

TransactionStatus

public interface TransactionStatus extends TransactionExecution, SavepointManager, Flushable{
/**
     * Return whether this transaction internally carries a savepoint,
     * that is, has been created as nested transaction based on a savepoint.
     * <p>This method is mainly here for diagnostic purposes, alongside
     * {@link #isNewTransaction()}. For programmatic handling of custom
     * savepoints, use the operations provided by {@link SavepointManager}.
     * @see #isNewTransaction()
     * @see #createSavepoint()
     * @see #rollbackToSavepoint(Object)
     * @see #releaseSavepoint(Object)
     */
boolean hasSavepoint();
Flush the underlying session to the datastore, if applicable: for example, all affected Hibernate/JPA sessions.
This is effectively just a hint and may be a no-op if the underlying transaction manager does not have a flush concept. A flush signal may get applied to the primary resource or to transaction synchronizations, depending on the underlying resource.
@Override
void flush();
}

他只定義了兩個(gè)方法:

  • hasSavepoint:返回這個(gè)事務(wù)是否包含了savepoint,也就是說,是否基于嵌套事務(wù)創(chuàng)建了一個(gè)savepoint。savepoint的概念前面的文章已經(jīng)分析過。
  • flush:這個(gè)應(yīng)該是和Hibernate或JPA相關(guān)的,具體作用暫時(shí)不管了,不研究Hibernate相關(guān)的東西。

沒了。

但是,這個(gè)接口繼承了3個(gè)接口:TransactionExecution, SavepointManager, Flushable,我們簡單看一眼:

TransactionExecution

這個(gè)接口很簡單,是事務(wù)狀態(tài)的一個(gè)通用接口,定義了當(dāng)前事務(wù)是否是一個(gè)新事務(wù)的獲取方法、設(shè)置當(dāng)前事務(wù)為回滾狀態(tài)、獲取事務(wù)是否已經(jīng)完成的方法等。

/**
 * Common representation of the current state of a transaction.
 * Serves as base interface for {@link TransactionStatus} as well as
 * {@link ReactiveTransaction}.
 *
 * @author Juergen Hoeller
 * @since 5.2
 */
public interface TransactionExecution {
    /**
     * Return whether the present transaction is new; otherwise participating
     * in an existing transaction, or potentially not running in an actual
     * transaction in the first place.
     */
    boolean isNewTransaction();
    /**
     * Set the transaction rollback-only. This instructs the transaction manager
     * that the only possible outcome of the transaction may be a rollback, as
     * alternative to throwing an exception which would in turn trigger a rollback.
     */
    void setRollbackOnly();
    /**
     * Return whether the transaction has been marked as rollback-only
     * (either by the application or by the transaction infrastructure).
     */
    boolean isRollbackOnly();
    /**
     * Return whether this transaction is completed, that is,
     * whether it has already been committed or rolled back.
     */
    boolean isCompleted();
}

SavepointManager

提供3個(gè)方法:創(chuàng)建保存點(diǎn)、回滾到保存點(diǎn)、釋放保存點(diǎn)。

Object createSavepoint() throws TransactionException;
void rollbackToSavepoint(Object savepoint) throws TransactionException;
void releaseSavepoint(Object savepoint) throws TransactionException;

Flushable

不說了,就是上面的那個(gè)flush方法。

AbstactTransactionStatus & DefaultTransactionStatus

AbstactTransactionStatus持有事務(wù)的幾個(gè)重要狀態(tài),業(yè)務(wù)執(zhí)行后,Spring事務(wù)管理器需要通過狀態(tài)判斷事務(wù)是提交或者是回滾。

private boolean rollbackOnly = false;
    private boolean completed = false;
    @Nullable
    private Object savepoint;

Spring事務(wù)管理機(jī)制中TransactionStatus的最終落地實(shí)現(xiàn)是DefaultTransactionStatus,代碼就不貼出了,比較簡單。

其實(shí)我們通過對TransactioStatus的分析能夠得出一個(gè)結(jié)論,那就是有savepoint的事務(wù)的回滾是通過TransactionStatus實(shí)現(xiàn)的。

TransactionStatus持有事務(wù)對象transaction,事務(wù)保存點(diǎn)savepoint是保存在transaction中,最終通過調(diào)用transaction的rollbackToSavepoint回滾事務(wù)到存儲點(diǎn)。

好了,TransactionStatus就分析到這兒。

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

相關(guān)文章

  • 淺談SpringBoot @Autowired的兩種注入方式

    淺談SpringBoot @Autowired的兩種注入方式

    本文主要介紹了兩種SpringBoot @Autowired注入方式,具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-06-06
  • Java中的@Builder注解問題詳解

    Java中的@Builder注解問題詳解

    這篇文章主要介紹了Java中的@Builder注解詳解,@Builder 注解的其中一個(gè)大坑會導(dǎo)致默認(rèn)值失效,這是使用此注解出現(xiàn)的一個(gè)問題,總的來說,不推薦再使用 @Builder 注解,接下來講重點(diǎn)介紹其原因和替代方案,需要的朋友可以參考下
    2023-10-10
  • java定時(shí)任務(wù)Timer和TimerTask使用詳解

    java定時(shí)任務(wù)Timer和TimerTask使用詳解

    這篇文章主要為大家詳細(xì)介紹了java定時(shí)任務(wù)Timer和TimerTask使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • idea全局設(shè)置Maven配置的實(shí)現(xiàn)步驟

    idea全局設(shè)置Maven配置的實(shí)現(xiàn)步驟

    本文主要介紹了idea全局設(shè)置Maven配置,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • java 如何計(jì)算同比增長工具類

    java 如何計(jì)算同比增長工具類

    這篇文章主要介紹了java 如何計(jì)算同比增長工具類的操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Kotlin 基本語法實(shí)例詳解

    Kotlin 基本語法實(shí)例詳解

    這篇文章主要介紹了Kotlin 基本語法實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • 一文徹底搞懂Java日期時(shí)間類詳解

    一文徹底搞懂Java日期時(shí)間類詳解

    這篇文章主要給大家介紹了關(guān)于Java日期時(shí)間類的相關(guān)資料,Calendar類的功能要比Date類強(qiáng)大很多,可以方便的進(jìn)行日期的計(jì)算,獲取日期中的信息時(shí)考慮了時(shí)區(qū)等問題,需要的朋友可以參考下
    2023-10-10
  • 在IDEA中安裝scala、maven、hadoop遇到的問題小結(jié)

    在IDEA中安裝scala、maven、hadoop遇到的問題小結(jié)

    這篇文章主要介紹了在IDEA中安裝scala、maven、hadoop遇到的問題小結(jié),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-10-10
  • RocketMQ?Broker實(shí)現(xiàn)高可用高并發(fā)的消息中轉(zhuǎn)服務(wù)

    RocketMQ?Broker實(shí)現(xiàn)高可用高并發(fā)的消息中轉(zhuǎn)服務(wù)

    RocketMQ消息代理(Broker)是一種高可用、高并發(fā)的消息中轉(zhuǎn)服務(wù),能夠接收并存儲生產(chǎn)者發(fā)送的消息,并將消息發(fā)送給消費(fèi)者。它具有多種消息存儲模式和消息傳遞模式,支持水平擴(kuò)展和故障轉(zhuǎn)移等特性,可以為分布式應(yīng)用提供可靠的消息傳遞服務(wù)
    2023-04-04
  • springboot-assembly自定義打包全過程

    springboot-assembly自定義打包全過程

    這篇文章主要介紹了springboot-assembly自定義打包全過程,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06

最新評論