Spring事務(wù)失效場(chǎng)景的詳細(xì)整理
前言
項(xiàng)目中用Spring的 @Transactional 注解控制事務(wù),使用中時(shí)常出現(xiàn)事物不生效的場(chǎng)景,本文僅限于日常項(xiàng)目開(kāi)發(fā)中的點(diǎn)滴整理總結(jié),總結(jié)以下幾點(diǎn),以備后續(xù)參考排查;可能不全,列舉出來(lái)希望可以幫助有需要的同學(xué),避免踩坑。
數(shù)據(jù)庫(kù)引擎不支持事物
這里以 MySQL 為例,其 MyISAM 引擎是不支持事務(wù)操作的,InnoDB 才是支持事務(wù)的引擎,一般要支持事務(wù)都會(huì)使用 InnoDB。
根據(jù) MySQL 的官方文檔:
https://dev.mysql.com/doc/refman/5.5/en/storage-engine-setting.html
從 MySQL 5.5.5 開(kāi)始的默認(rèn)存儲(chǔ)引擎是:InnoDB,之前默認(rèn)的都是:MyISAM,所以這點(diǎn)要值得注意,底層引擎不支持事務(wù)是硬傷。
沒(méi)有被 Spring 管理
// @Service (此注解不能去掉)
public class AccountServiceImpl implements AccountService {
@Transactional
public void inser(Account account) {
// insert account
}
}如果此時(shí)把 @Service 注解注釋掉,這個(gè)類(lèi)就不會(huì)被加載成一個(gè) Bean,那這個(gè)類(lèi)就不會(huì)被 Spring 管理了,事務(wù)自然就失效了。
方法不是 public 的
以下來(lái)自于Spring 官方文檔:
When using proxies, you should apply the @Transactional annotation only to methods with public visibility. If you do annotate protected, private or package-visible methods with the @Transactional annotation, no error is raised, but the annotated method does not exhibit the configured transactional settings. Consider the use of AspectJ (see below) if you need to annotate non-public methods.
意思就是 @Transactional 只能用于 public 的方法上,否則事務(wù)不會(huì)失效,如果要用在非 public 方法上,可以考慮開(kāi)啟 AspectJ 代理模式。
自身調(diào)用問(wèn)題
看下面代碼
@Service
public class AccountServiceImpl implements AccountService {
public void insert(Account account) {
insertAccount(account);
}
@Transactional
public void insertAccount(Account account) {
// insert account
}
}
insert方法上面沒(méi)有加 @Transactional 注解,調(diào)用有 @Transactional 注解的 insertAccount 方法,insertAccount 方法上的事務(wù)其實(shí)是不管用的。
再看下面的代碼
@Service
public class AccountServiceImpl implements AccountService {
@Transactional
public void insert(Account account) {
insertAccount(account);
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void insertAccount(Account account) {
// insert account
}
}
這次在 insert 方法上加了 @Transactional,insertAccount 加了 REQUIRES_NEW 新開(kāi)啟一個(gè)事務(wù),那么新開(kāi)的事務(wù)管用么?
這兩個(gè)例子的答案是:不管用!
因?yàn)樗鼈儼l(fā)生了自身調(diào)用,就調(diào)該類(lèi)自己的方法,而沒(méi)有經(jīng)過(guò) Spring 的代理類(lèi),默認(rèn)只有在外部調(diào)用事務(wù)才會(huì)生效,這也是老生常談的經(jīng)典問(wèn)題了。
數(shù)據(jù)源沒(méi)有配置事物管理器
@Bean
public PlatformTransactionManager transactionManager(DataSource dataSource)
{
return new DataSourceTransactionManager(dataSource);
}
如上面所示,當(dāng)前數(shù)據(jù)源若沒(méi)有配置事務(wù)管理器,照樣會(huì)失效!
不支持事物
@Service
public class AccountServiceImpl implements AccountService {
@Transactional
public void insert(Account account) {
insertAccount(account);
}
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void insertAccount(Account account) {
// insert account
}
}
Propagation.NOT_SUPPORTED: 表示不以事務(wù)運(yùn)行,當(dāng)前若存在事務(wù)則掛起,詳細(xì)的可以參考InnoDB的事務(wù)隔離級(jí)別和傳播機(jī)制。
都主動(dòng)不支持以事務(wù)方式運(yùn)行了,那事務(wù)生效也是白搭!
異常被吃掉
這個(gè)是比較常見(jiàn)的場(chǎng)景
@Service (此注解不能去掉)
public class AccountServiceImpl implements AccountService {
@Transactional
public void inser(Account account) {
try {
// insert account
} catch {
}
}
}
把異常吃了,然后又不拋出來(lái),事務(wù)就無(wú)法回滾!
異常類(lèi)型錯(cuò)誤
@Service (此注解不能去掉)
public class AccountServiceImpl implements AccountService {
@Transactional
public void inser(Account account) {
try {
// insert account
} catch {
throw new Exception("新增錯(cuò)誤");
}
}
}
這樣事務(wù)也是不生效的,因?yàn)槟J(rèn)回滾的是:RuntimeException,如果你想觸發(fā)其他異常的回滾,需要在注解上配置一下,如:
@Transactional(rollbackFor = Exception.class)
這個(gè)配置僅限于 Throwable 異常類(lèi)及其子類(lèi)。
查閱資料,其他失效的場(chǎng)景需注意:1) 像文件導(dǎo)入數(shù)據(jù)庫(kù),用多線程控制;可參考查詢spring 多線程事務(wù)的問(wèn)題 2)SpringBoot+Shiro引起事務(wù)失效
總結(jié)
本文總結(jié)了幾種事務(wù)失效的場(chǎng)景,其實(shí)發(fā)生最多就是自身調(diào)用、異常被吃、異常拋出類(lèi)型不對(duì)這三個(gè)了;像文章開(kāi)頭說(shuō)的那樣,本文不一定總結(jié)得全,只是根據(jù)經(jīng)驗(yàn)總結(jié)常見(jiàn)的事務(wù)失效的場(chǎng)景。
到此這篇關(guān)于Spring事務(wù)失效場(chǎng)景的文章就介紹到這了,更多相關(guān)Spring事務(wù)失效場(chǎng)景內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Nacos docker單機(jī)模式部署實(shí)現(xiàn)過(guò)程詳解
這篇文章主要介紹了Nacos docker單機(jī)模式部署實(shí)現(xiàn)過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
Spring聲明式事務(wù)@Transactional知識(shí)點(diǎn)分享
在本篇文章里小編給大家整理了關(guān)于Spring聲明式事務(wù)@Transactional詳解內(nèi)容,需要的朋友們可以參考下。2020-02-02
java基于servlet使用組件smartUpload實(shí)現(xiàn)文件上傳
這篇文章主要介紹了java基于servlet使用組件smartUpload實(shí)現(xiàn)文件上傳,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
Spring通過(guò)配置文件和注解實(shí)現(xiàn)屬性賦值
這篇文章主要介紹了Spring通過(guò)配置文件和注解實(shí)現(xiàn)屬性賦值,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
Java數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)之棧和隊(duì)列
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)之棧和隊(duì)列,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有一定的幫助,需要的朋友可以參考下2021-05-05
JAVA獲取當(dāng)前項(xiàng)目和文件所在路徑的實(shí)例代碼
這篇文章主要介紹了JAVA獲取當(dāng)前項(xiàng)目和文件所在路徑的實(shí)例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03

