Spring事務(wù)框架之TransactionStatus源碼解析
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注入方式,具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-06-06java定時(shí)任務(wù)Timer和TimerTask使用詳解
這篇文章主要為大家詳細(xì)介紹了java定時(shí)任務(wù)Timer和TimerTask使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02idea全局設(shè)置Maven配置的實(shí)現(xiàn)步驟
本文主要介紹了idea全局設(shè)置Maven配置,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07在IDEA中安裝scala、maven、hadoop遇到的問題小結(jié)
這篇文章主要介紹了在IDEA中安裝scala、maven、hadoop遇到的問題小結(jié),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10RocketMQ?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