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í)我們通過對(duì)TransactioStatus的分析能夠得出一個(gè)結(jié)論,那就是有savepoint的事務(wù)的回滾是通過TransactionStatus實(shí)現(xiàn)的。
TransactionStatus持有事務(wù)對(duì)象transaction,事務(wù)保存點(diǎn)savepoint是保存在transaction中,最終通過調(diào)用transaction的rollbackToSavepoint回滾事務(wù)到存儲(chǔ)點(diǎn)。
好了,TransactionStatus就分析到這兒。
以上就是Spring事務(wù)框架之TransactionStatus的詳細(xì)內(nèi)容,更多關(guān)于Spring事務(wù)TransactionStatus的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
使用SpringBoot進(jìn)行身份驗(yàn)證和授權(quán)的示例詳解
在廣闊的 Web 開發(fā)世界中,身份驗(yàn)證是每個(gè)數(shù)字領(lǐng)域的守護(hù)者,在本教程中,我們將了解如何以本機(jī)方式保護(hù)、驗(yàn)證和授權(quán) Spring-Boot 應(yīng)用程序的用戶,并遵循框架的良好實(shí)踐,希望對(duì)大家有所幫助2023-11-11
Java登錄功能實(shí)現(xiàn)token生成與驗(yàn)證
這篇文章介紹了Java登錄功能實(shí)現(xiàn)token生成與驗(yàn)證,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12
linux部署出現(xiàn)java文件操作報(bào)錯(cuò):java.io.FileNotFoundException解決辦法
這篇文章主要g介紹了linux部署出現(xiàn)java文件操作報(bào)錯(cuò):java.io.FileNotFoundException解決的相關(guān)資料,這個(gè)錯(cuò)誤通常表示你的Spring Boot應(yīng)用程序無法找到指定的文本文件,需要的朋友可以參考下2023-12-12
SpringSecurity+Redis+Jwt實(shí)現(xiàn)用戶認(rèn)證授權(quán)
SpringSecurity是一個(gè)強(qiáng)大且靈活的身份驗(yàn)證和訪問控制框架,本文主要介紹了SpringSecurity+Redis+Jwt實(shí)現(xiàn)用戶認(rèn)證授權(quán),具有一定的參考價(jià)值,感興趣的可以了解一下2024-07-07
Spring?Data?JPA?注解Entity關(guān)聯(lián)關(guān)系使用詳解
這篇文章主要為大家介紹了Spring?Data?JPA?注解Entity關(guān)聯(lián)關(guān)系使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09

