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

Spring事務框架之TransactionStatus源碼解析

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

Spring事務框架

本文來分析TransactionStatus。

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

先來看一下TransactionStatus的類結構:

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();
}

他只定義了兩個方法:

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

沒了。

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

TransactionExecution

這個接口很簡單,是事務狀態(tài)的一個通用接口,定義了當前事務是否是一個新事務的獲取方法、設置當前事務為回滾狀態(tài)、獲取事務是否已經(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個方法:創(chuàng)建保存點、回滾到保存點、釋放保存點。

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

Flushable

不說了,就是上面的那個flush方法。

AbstactTransactionStatus & DefaultTransactionStatus

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

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

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

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

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

好了,TransactionStatus就分析到這兒。

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

相關文章

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

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

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

    Java中的@Builder注解問題詳解

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

    java定時任務Timer和TimerTask使用詳解

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

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

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

    java 如何計算同比增長工具類

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

    Kotlin 基本語法實例詳解

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

    一文徹底搞懂Java日期時間類詳解

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

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

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

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

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

    springboot-assembly自定義打包全過程

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

最新評論