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注入方式,具有一定的參考價值,需要的朋友們下面隨著小編來一起學習學習吧2021-06-06在IDEA中安裝scala、maven、hadoop遇到的問題小結
這篇文章主要介紹了在IDEA中安裝scala、maven、hadoop遇到的問題小結,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10RocketMQ?Broker實現(xiàn)高可用高并發(fā)的消息中轉(zhuǎn)服務
RocketMQ消息代理(Broker)是一種高可用、高并發(fā)的消息中轉(zhuǎn)服務,能夠接收并存儲生產(chǎn)者發(fā)送的消息,并將消息發(fā)送給消費者。它具有多種消息存儲模式和消息傳遞模式,支持水平擴展和故障轉(zhuǎn)移等特性,可以為分布式應用提供可靠的消息傳遞服務2023-04-04