偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Spring事務(wù)失效場景匯總

開發(fā) 前端
項目中用Spring的 @Transactional 注解控制事務(wù),使用中時常出現(xiàn)事物不生效的場景,本文僅限于日常項目開發(fā)中的點滴整理總結(jié)。

項目中用Spring的 @Transactional 注解控制事務(wù),使用中時常出現(xiàn)事物不生效的場景,本文僅限于日常項目開發(fā)中的點滴整理總結(jié),總結(jié)以下幾點,以備后續(xù)參考排查;可能不全,列舉出來希望可以幫助有需要的同學,避免踩坑。

數(shù)據(jù)庫引擎不支持事物

這里以 MySQL 為例,其 MyISAM 引擎是不支持事務(wù)操作的,InnoDB 才是支持事務(wù)的引擎,一般要支持事務(wù)都會使用 InnoDB。

根據(jù) MySQL 的官方文檔:

https://dev.mysql.com/doc/refman/5.5/en/storage-engine-setting.html

從 MySQL 5.5.5 開始的默認存儲引擎是:InnoDB,之前默認的都是:MyISAM,所以這點要值得注意,底層引擎不支持事務(wù)是硬傷。

沒有被 Spring 管理

// @Service (此注解不能去掉)
public class AccountServiceImpl implements AccountService {
@Transactional
public void inser(Account account) {
// insert account
}
}

如果此時把 @Service 注解注釋掉,這個類就不會被加載成一個 Bean,那這個類就不會被 Spring 管理了,事務(wù)自然就失效了。

方法不是 public 的

以下來自于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ù)不會失效,如果要用在非 public 方法上,可以考慮開啟 AspectJ 代理模式。

自身調(diào)用問題

看下面代碼

@Service
public class AccountServiceImpl implements AccountService {
public void insert(Account account) {
insertAccount(account);
}
@Transactional
public void insertAccount(Account account) {
// insert account
}
}

insert方法上面沒有加 @Transactional 注解,調(diào)用有 @Transactional 注解的 insertAccount 方法,insertAccount 方法上的事務(wù)其實是不管用的。

再看下面的代碼

@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 新開啟一個事務(wù),那么新開的事務(wù)管用么?

這兩個例子的答案是:不管用!

因為它們發(fā)生了自身調(diào)用,就調(diào)該類自己的方法,而沒有經(jīng)過 Spring 的代理類,默認只有在外部調(diào)用事務(wù)才會生效,這也是老生常談的經(jīng)典問題了。

數(shù)據(jù)源沒有配置事物管理器

@Bean
public PlatformTransactionManager transactionManager(DataSource dataSource)
{
return new DataSourceTransactionManager(dataSource);
}

如上面所示,當前數(shù)據(jù)源若沒有配置事務(wù)管理器,照樣會失效!

不支持事物

@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ù)運行,當前若存在事務(wù)則掛起,詳細的可以參考InnoDB的事務(wù)隔離級別和傳播機制。

都主動不支持以事務(wù)方式運行了,那事務(wù)生效也是白搭!

異常被吃掉

這個是比較常見的場景

@Service (此注解不能去掉)
public class AccountServiceImpl implements AccountService {
@Transactional
public void inser(Account account) {
try {
// insert account
} catch {
}
}
}

把異常吃了,然后又不拋出來,事務(wù)就無法回滾!

異常類型錯誤

@Service (此注解不能去掉)
public class AccountServiceImpl implements AccountService {
@Transactional
public void inser(Account account) {
try {
// insert account
} catch {
throw new Exception("新增錯誤");
}
}
}

這樣事務(wù)也是不生效的,因為默認回滾的是:RuntimeException,如果你想觸發(fā)其他異常的回滾,需要在注解上配置一下,如:

@Transactional(rollbackFor = Exception.class)

這個配置僅限于 Throwable 異常類及其子類。

查閱資料,其他失效的場景需注意:1) 像文件導入數(shù)據(jù)庫,用多線程控制;可參考查詢spring 多線程事務(wù)的問題 2)SpringBoot+Shiro引起事務(wù)失效

總結(jié)

本文總結(jié)了幾種事務(wù)失效的場景,其實發(fā)生最多就是自身調(diào)用、異常被吃、異常拋出類型不對這三個了;像文章開頭說的那樣,本文不一定總結(jié)得全,只是根據(jù)經(jīng)驗總結(jié)常見的事務(wù)失效的場景,如有遺漏,歡迎大佬們留言分享。

責任編輯:未麗燕 來源: 今日頭條
相關(guān)推薦

2024-01-29 08:28:01

Spring事務(wù)失效

2024-09-09 08:29:25

2021-12-13 11:12:41

Spring事務(wù)失效

2021-09-04 07:56:44

Spring事務(wù)失效

2025-02-10 00:27:54

2023-07-05 08:45:18

Spring事務(wù)失效場景

2022-04-13 20:53:15

Spring事務(wù)管理

2022-07-05 14:19:30

Spring接口CGLIB

2023-09-28 09:07:54

注解失效場景

2022-09-22 09:57:20

Spring事務(wù)失效

2022-12-06 10:39:43

Spring事務(wù)失效

2021-04-14 15:17:08

Transaction代碼語言

2022-03-04 21:06:46

spring事務(wù)失效

2022-08-09 09:34:32

Spring開發(fā)

2022-08-08 17:38:45

Spring策略事務(wù)

2021-04-15 08:01:27

Spring聲明式事務(wù)

2023-05-26 07:19:49

Spring聲明式事務(wù)

2022-05-26 08:23:05

MySQL索引數(shù)據(jù)庫

2020-11-12 19:30:37

Spring失效事務(wù)

2023-06-30 07:58:07

Spring數(shù)據(jù)源事務(wù)
點贊
收藏

51CTO技術(shù)棧公眾號