Spring事務(wù)框架之TransactionDefinition源碼解析
Spring事務(wù)的底層原理
- Spring事務(wù)的框架。
- Spring事務(wù)引入機(jī)制,主要學(xué)習(xí)@EnableTransactionManagement的相關(guān)內(nèi)容。
- Spring事務(wù)的控制機(jī)制,主要學(xué)習(xí)@Trasactional相關(guān)內(nèi)容。
先從Spring事務(wù)的框架開(kāi)始。
Spring事務(wù)框架中最重要的內(nèi)容主要包括:
- 事務(wù)管理器TransactionManager。
- 事務(wù)定義TransactionDefinition。
- 事務(wù)狀態(tài)TransactionStatus。
其中事務(wù)管理器是核心,研究事務(wù)管理器需要用到TransactionDefinition和TransactionStatus,所以我們先研究這兩部分。
TransactionDefinition
顧名思義,TransactionDefinition是用來(lái)定義事務(wù)屬性的。
有兩個(gè)比較重要的實(shí)現(xiàn)類,一個(gè)是DefaultTransactionAttribute.

另一個(gè)是DefaultTransactionTemplate:

下來(lái)看一下TransactionDefinition中定義的幾個(gè)事務(wù)屬性:
事務(wù)傳播機(jī)制,7個(gè)事務(wù)傳播機(jī)制的具體含義,我們?cè)谄渌恼轮幸呀?jīng)分析過(guò)了:
int PROPAGATION_REQUIRED = 0; int PROPAGATION_SUPPORTS = 1; int PROPAGATION_MANDATORY = 2; int PROPAGATION_REQUIRES_NEW = 3; int PROPAGATION_NOT_SUPPORTED = 4; int PROPAGATION_NEVER = 5; int PROPAGATION_NESTED = 6;
隔離級(jí)別
數(shù)據(jù)庫(kù)隔離級(jí)別:
//數(shù)據(jù)庫(kù)的默認(rèn)隔離級(jí)別 int ISOLATION_DEFAULT = -1; int ISOLATION_READ_UNCOMMITTED = 1; int ISOLATION_READ_COMMITTED = 2; int ISOLATION_REPEATABLE_READ = 4; int ISOLATION_SERIALIZABLE = 8;
默認(rèn)超時(shí)
int TIMEOUT_DEFAULT = -1;
然后,有一個(gè)獲取默認(rèn)事務(wù)定義的方法(用意待考):
static TransactionDefinition withDefaults() {
return StaticTransactionDefinition.INSTANCE;
}DefaultTransactionAttribute
其中比較重要的一個(gè)方式是rollbackOn:
public boolean rollbackOn(Throwable ex) {
return (ex instanceof RuntimeException || ex instanceof Error);
}我們先來(lái)看看JavaDoc:
The default behavior is as with EJB: rollback on unchecked exception (RuntimeException), assuming an unexpected outcome outside of any business rules. Additionally, we also attempt to rollback on Error which is clearly an unexpected outcome as well. By contrast, a checked exception is considered a business exception and therefore a regular expected outcome of the transactional business method, i.e. a kind of alternative return value which still allows for regular completion of resource operations.
This is largely consistent with TransactionTemplate's default behavior, except that TransactionTemplate also rolls back on undeclared checked exceptions (a corner case). For declarative transactions, we expect checked exceptions to be intentionally declared as business exceptions, leading to a commit by default.
默認(rèn)行為與EJB相同:發(fā)生運(yùn)行時(shí)異?;虬l(fā)生Error時(shí)回滾。而對(duì)于check exception一般會(huì)被認(rèn)為是一個(gè)業(yè)務(wù)可以認(rèn)可或期望的結(jié)果,因此是允許交易完成的。這個(gè)默認(rèn)行為與TransactionTemplate的默認(rèn)行為是保持一致的。
大概的意思是說(shuō):默認(rèn)情況下發(fā)生運(yùn)行時(shí)異?;駿rror的時(shí)候事務(wù)回滾。而這個(gè)默認(rèn)行為就是通過(guò)這個(gè)rollbackOn方法來(lái)保證的:
public boolean rollbackOn(Throwable ex) {
return (ex instanceof RuntimeException || ex instanceof Error);
}Spring事務(wù)控制中的事務(wù)定義的最終落地實(shí)現(xiàn)就是這個(gè)TransactionAttribute,不過(guò)他是通過(guò)TransactionAttributeSource持有的,后續(xù)分析Spring事務(wù)啟用機(jī)制@EnalbeTransactionManagment的時(shí)候會(huì)看到。
TransactionDefinition的研究就到這里,下一篇文章研究TransactionStatus。
以上就是Spring事務(wù)框架之TransactionDefinition源碼解析的詳細(xì)內(nèi)容,更多關(guān)于Spring事務(wù)TransactionDefinition的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
基于Spring Boot 的小區(qū)人臉識(shí)別與出入記錄管理系統(tǒng)功能
文章介紹基于SpringBoot框架與百度AI人臉識(shí)別API的小區(qū)出入管理系統(tǒng),實(shí)現(xiàn)自動(dòng)識(shí)別、記錄及查詢功能,涵蓋技術(shù)選型、數(shù)據(jù)模型設(shè)計(jì)、接口開(kāi)發(fā)與系統(tǒng)優(yōu)化方案,為智慧社區(qū)提供高效安全管理工具,感興趣的朋友跟隨小編一起看看吧2025-08-08
Maven項(xiàng)目中resources配置總結(jié)
這篇文章主要介紹了Maven項(xiàng)目中resources配置總結(jié),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
淺談Java讀寫注冊(cè)表的方式Preferences與jRegistry
這篇文章主要介紹了淺談Java讀寫注冊(cè)表的方式Preferences與jRegistry,分享了相關(guān)代碼示例,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02
Java concurrency線程池之線程池原理(一)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了Java concurrency線程池之線程池原理,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
Jmeter參數(shù)化實(shí)現(xiàn)原理及過(guò)程解析
這篇文章主要介紹了Jmeter參數(shù)化實(shí)現(xiàn)原理及過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
Jasypt的StandardPBEByteEncryptor使用源碼解析
這篇文章主要介紹了Jasypt的StandardPBEByteEncryptor使用源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09

