工作六年,@Transactional 注解用的一塌糊涂
接手新項目一言難盡,別的不說單單就一個 @Transactional 注解用的一塌糊涂,五花八門的用法,很大部分還失效無法回滾。
圖片
有意識的在涉及事務相關方法上加@Transactional注解,是個好習慣。不過,很多同學只是下意識地添加這個注解,一旦功能正常運行,很少有人會深入驗證異常情況下事務是否能正確回滾。@Transactional 注解雖然用起來簡單,但這貨總是能在一些你意想不到的情況下失效,防不勝防!
我把這些事務問題歸結(jié)成了三類:不必要、不生效、不回滾,接下用一些demo演示下各自的場景。
不必要
1. 無需事務的業(yè)務
在沒有事務操作的業(yè)務方法上使用 @Transactional 注解,比如:用在僅有查詢或者一些 HTTP 請求的方法,雖然加上影響不大,但從編碼規(guī)范的角度來看還是不夠嚴謹,建議去掉。
@Transactional
public String testQuery() {
standardBak2Service.getById(1L);
return "testB";
}
2. 事務范圍過大
有些同學為了省事直接將 @Transactional 注解加在了類上或者抽象類上,這樣做導致的問題就是類內(nèi)的方法或抽象類的實現(xiàn)類中所有方法全部都被事務管理。增加了不必要的性能開銷或復雜性,建議按需使用,只在有事務邏輯的方法上加@Transactional。
@Transactional
public abstract class BaseService {
}
@Slf4j
@Service
public class TestMergeService extends BaseService{
private final TestAService testAService;
public String testMerge() {
testAService.testA();
return "ok";
}
}
如果在類中的方法上添加 @Transactional 注解,它將覆蓋類級別的事務配置。例如,類級別上配置了只讀事務,方法級別上的 @Transactional 注解也會覆蓋該配置,從而啟用讀寫事務。
@Transactional(readOnly = true)
public class TestMergeService {
private final TestBService testBService;
private final TestAService testAService;
@Transactional
public String testMerge() {
testAService.testA();
testBService.testB();
return "ok";
}
}
不生效
3. 方法權限問題
不要把 @Transactional注解加在 private 級別的方法上!
我們知道 @Transactional 注解依賴于Spring AOP切面來增強事務行為,這個 AOP 是通過代理來實現(xiàn)的,而 private 方法恰恰不能被代理的,所以 AOP 對 private 方法的增強是無效的,@Transactional也就不會生效。
@Transactional
private String testMerge() {
testAService.testA();
testBService.testB();
return "ok";
}
那如果我在 testMerge() 方法內(nèi)調(diào)用 private 的方法事務會生效嗎?
答案:事務會生效
@Transactional
public String testMerge() throws Exception {
ccc();
return "ok";
}
private void ccc() {
testAService.testA();
testBService.testB();
}
4. 被用 final 、static 修飾方法
和上邊的原因類似,被用 final 、static 修飾的方法上加 @Transactional 也不會生效。
- static 靜態(tài)方法屬于類本身的而非實例,因此代理機制是無法對靜態(tài)方法進行代理或攔截的
- final 修飾的方法不能被子類重寫,事務相關的邏輯無法插入到 final 方法中,代理機制無法對 final 方法進行攔截或增強。
這些都是java基礎概念了,使用時要注意。
@Transactional
public static void b() {
}
@Transactional
public final void b() {
}
5. 同類內(nèi)部方法調(diào)用問題
注意了,這種情況經(jīng)常發(fā)生??!
同類內(nèi)部方法間的調(diào)用是 @Transactional 注解失效的重災區(qū),網(wǎng)上你總能看到方法內(nèi)部調(diào)用另一個同類的方法時,這種調(diào)用是不會經(jīng)過代理的,因此事務管理不會生效。但這說法比較片面,要分具體情況。
比如:testMerge() 方法開啟事務,調(diào)用同類非事務的方法 a() 和 b() ,此時 b() 拋異常,根據(jù)事務的傳播性 a()、b() 事務均生效。
@Transactional
public String testMerge() {
a();
b();
return "ok";
}
public void a() {
standardBakService.save(testAService.buildEntity());
}
public void b() {
standardBak2Service.save(testBService.buildEntity2());
throw new RuntimeException("b error");
}
如果 testMerge() 方法未開啟事務,并且在同類中調(diào)用了非事務方法 a() 和事務方法 b(),當 b() 拋出異常時,a() 和 b() 的事務都不會生效。因為這種調(diào)用直接通過 this 對象進行,未經(jīng)過代理,因此事務管理無法生效。這經(jīng)常出問題的!
public String testMerge() {
a();
b();
return "ok";
}
public void a() {
standardBakService.save(testAService.buildEntity());
}
@Transactional
public void b() {
standardBak2Service.save(testBService.buildEntity2());
throw new RuntimeException("b error");
}
5.1 獨立的 Service 類
要想 b() 方法的事務生效也容易,最簡單的方法將它剝離放在獨立的Service類注入使用,交給spring管理就行了。不過,這種方式會創(chuàng)建很多類。
@Slf4j
@Service
public class TestBService {
@Transactional
public void b() {
standardBak2Service.save(testBService.buildEntity2());
throw new RuntimeException("b error");
}
}
5.2 自注入方式
或者通過自己注入自己的方式解決,盡管解決了問題,邏輯看起來很奇怪,它破壞了依賴注入的原則,雖然 spring 支持我們這樣用,還是要注意下循環(huán)依賴的問題。
@Slf4j
@Service
public class TestMergeService {
@Autowired
private TestMergeService testMergeService;
public String testMerge() {
a();
testMergeService.b();
return "ok";
}
public void a() {
standardBakService.save(testAService.buildEntity());
}
@Transactional
public void b() {
standardBak2Service.save(testBService.buildEntity2());
throw new RuntimeException("b error");
}
}
5.3 手動獲取代理對象
b() 方法它不是沒被代理嘛,那我們手動獲取代理對象調(diào)用 b() 方法也可以。通過 AopContext.currentProxy() 方法返回當前的代理對象實例,這樣調(diào)用代理的方法時,就會經(jīng)過 AOP 的切面,@Transactional注解就會生效了。
@Slf4j
@Service
public class TestMergeService {
public String testMerge() {
a();
((TestMergeService) AopContext.currentProxy()).b();
return "ok";
}
public void a() {
standardBakService.save(testAService.buildEntity());
}
@Transactional
public void b() {
standardBak2Service.save(testBService.buildEntity2());
throw new RuntimeException("b error");
}
}
6. Bean 未被 spring 管理
上邊我們知道 @Transactional 注解通過 AOP 來管理事務,而 AOP 依賴于代理機制。因此,Bean 必須由Spring管理實例! 要確保為類加上如 @Controller、@Service 或 @Component注解,讓其被Spring所管理,這很容易忽視。
@Service
public class TestBService {
@Transactional
public String testB() {
standardBak2Service.save(entity2);
return "testB";
}
}
7. 異步線程調(diào)用
如果我們在 testMerge() 方法中使用異步線程執(zhí)行事務操作,通常也是無法成功回滾的,來個具體的例子。
testMerge() 方法在事務中調(diào)用了 testA(),testA() 方法中開啟了事務。接著,在 testMerge() 方法中,我們通過一個新線程調(diào)用了 testB(),testB() 中也開啟了事務,并且在 testB() 中拋出了異常。
此時的回滾情況是怎樣的呢?
@Transactional
public String testMerge() {
testAService.testA();
new Thread(() -> {
try {
testBService.testB();
} catch (Exception e) {
// e.printStackTrace();
throw new RuntimeException();
}
}).start();
return "ok";
}
@Transactional
public String testB() {
DeepzeroStandardBak2 entity2 = buildEntity2();
dataImportJob2Service.save(entity2);
throw new RuntimeException("test2");
}
@Transactional
public String testA() {
DeepzeroStandardBak entity = buildEntity();
standardBakService.save(entity);
return "ok";
}
答案是:testA() 和 testB() 中的事務都不會回滾。
testA() 無法回滾是因為沒有捕獲到新線程中 testB()拋出的異常;testB()方法無法回滾,是因為事務管理器只對當前線程中的事務有效,因此在新線程中執(zhí)行的事務不會回滾。
由于在多線程環(huán)境下,Spring 的事務管理器不會跨線程傳播事務,事務的狀態(tài)(如事務是否已開啟)是存儲在線程本地的 ThreadLocal 來存儲和管理事務上下文信息。這意味著每個線程都有一個獨立的事務上下文,事務信息在不同線程之間不會共享。
8. 不支持事務的引擎
不支持事務的數(shù)據(jù)庫引擎不在此次 Review 范圍內(nèi),只做了解就好。我們通常使用的關系型數(shù)據(jù)庫,如 MySQL,默認使用支持事務的 InnoDB 引擎,而非事務的 MyISAM 引擎則使用較少。
以前開啟啟用 MyISAM 引擎是為了提高查詢效率。不過,現(xiàn)在非關系型數(shù)據(jù)庫如 Redis、MongoDB 和 Elasticsearch 等中間件提供了更高性價比的解決方案。
不回滾
9. 用錯傳播屬性
@Transactional注解有個關鍵的參數(shù)propagation,它控制著事務的傳播行為,有時事務傳播參數(shù)配置錯誤也會導致事務的不回滾。
propagation 支持 7 種事務傳播特性:
- REQUIRED:默認的傳播行為,如果當前沒有事務,則創(chuàng)建一個新事務;如果存在事務,則加入當前事務。
- MANDATORY:支持當前事務,如果不存在則拋出異常
- NEVER:非事務性執(zhí)行,如果存在事務,則拋出異常
- REQUIRES_NEW:無論當前是否存在事務,都會創(chuàng)建一個新事務,原有事務被掛起。
- NESTED:嵌套事務,被調(diào)用方法在一個嵌套的事務中運行,這個事務依賴于當前的事務。
- SUPPORTS:如果當前存在事務,則加入;如果沒有,就以非事務方式執(zhí)行。
- NOT_SUPPORTED:以非事務方式執(zhí)行,如果當前存在事務,將其掛起。
為了加深印象,我用案例來模擬下每種特性的使用場景。
REQUIRED
REQUIRED 是默認的事務傳播行為。如果 testMerge() 方法開啟了事務,那么其內(nèi)部調(diào)用的 testA() 和 testB() 方法也將加入這個事務。如果 testMerge() 沒有開啟事務,而 testA() 和 testB() 方法上使用了 @Transactional 注解,這些方法將各自創(chuàng)建新的事務,只控制自身的回滾。
@Component
@RequiredArgsConstructor
@Slf4j
@Service
public class TestMergeService {
private final TestBService testBService;
private final TestAService testAService;
@Transactional
public String testMerge() {
testAService.testA();
testBService.testB();
return "ok";
}
}
@Transactional
public String testA() {
log.info("testA");
DeepzeroStandardBak entity = buildEntity();
standardBakService.save(entity);
return "ok";
}
@Transactional
public String testB() {
log.info("testB");
DeepzeroStandardBak2 entity2 = buildEntity2();
standardBak2Service.save(entity2);
throw new RuntimeException("testB");
}
MANDATORY
MANDATORY 傳播特性簡單來說就是只能被開啟事務的上層方法調(diào)用,例如 testMerge() 方法未開啟事務調(diào)用 testB() 方法,那么將拋出異常;testMerge() 開啟事務調(diào)用 testB() 方法,則加入當前事務。
@Component
@RequiredArgsConstructor
@Slf4j
@Service
public class TestMergeService {
private final TestBService testBService;
private final TestAService testAService;
public String testMerge() {
testAService.testA();
testBService.testB();
return "ok";
}
}
@Transactional
public String testA() {
log.info("testA");
DeepzeroStandardBak entity = buildEntity();
standardBakService.save(entity);
return "ok";
}
@Transactional(propagation = Propagation.MANDATORY)
public String testB() {
log.info("testB");
DeepzeroStandardBak2 entity2 = buildEntity2();
standardBak2Service.save(entity2);
throw new RuntimeException("testB");
}
拋出的異常信息
org.springframework.transaction.IllegalTransactionStateException: No existing transaction found for transaction marked with propagation 'mandatory'
NEVER
NEVER 傳播特性是強制你的方法只能以非事務方式運行,如果方法存在事務操作會拋出異常,我實在是沒想到有什么使用場景。
@Transactional(propagation = Propagation.NEVER)
public String testB() {
log.info("testB");
DeepzeroStandardBak2 entity2 = buildEntity2();
standardBak2Service.save(entity2);
// throw new RuntimeException("testB");
return "ok";
}
拋出的異常信息
org.springframework.transaction.IllegalTransactionStateException: Existing transaction found for transaction marked with propagation 'never'
REQUIRES_NEW
我們在使用 Propagation.REQUIRES_NEW 傳播特性時,不論當前事務的狀態(tài)如何,調(diào)用該方法都會創(chuàng)建一個新的事務。
例如,testMerge() 方法開始一個事務,調(diào)用 testB() 方法時,它會暫停 testMerge() 的事務,并啟動一個新的事務。如果 testB() 方法內(nèi)部發(fā)生異常,新事務會回滾,但原先掛起的事務不會受影響。這意味著,掛起的事務不會因為新事務的回滾而受到影響,也不會因為新事務的失敗而回滾。
@Transactional
public String testMerge() {
testAService.testA();
testBService.testB();
return "ok";
}
@Transactional
public String testA() {
log.info("testA");
DeepzeroStandardBak entity = buildEntity();
standardBakService.save(entity);
return "ok";
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
public String testB() {
log.info("testB");
DeepzeroStandardBak2 entity2 = buildEntity2();
standardBak2Service.save(entity2);
throw new RuntimeException("testB");
}
NESTED
方法的傳播行為設置為 NESTED,其內(nèi)部方法會開啟一個新的嵌套事務(子事務)。在沒有外部事務的情況下 NESTED 與 REQUIRED 效果相同;存在外部事務的情況下,一旦外部事務回滾,它會創(chuàng)建一個嵌套事務(子事務)。
也就是說外部事務回滾時,子事務會跟著回滾;但子事務的回滾不會對外部事務和其他同級事務造成影響。
@Component
@RequiredArgsConstructor
@Slf4j
@Service
public class TestMergeService {
private final TestBService testBService;
private final TestAService testAService;
@Transactional
public String testMerge() {
testAService.testA();
testBService.testB();
throw new RuntimeException("testMerge");
return "ok";
}
}
@Transactional
public String testA() {
log.info("testA");
DeepzeroStandardBak entity = buildEntity();
standardBakService.save(entity);
return "ok";
}
@Transactional(propagation = Propagation.NESTED)
public String testB() {
log.info("testB");
DeepzeroStandardBak2 entity2 = buildEntity2();
standardBak2Service.save(entity2);
throw new RuntimeException("testB");
}
NOT_SUPPORTED
NOT_SUPPORTED 事務傳播特性表示該方法必須以非事務方式運行。當方法 testMerge() 開啟事務并調(diào)用事務方法 testA() 和 testB() 時,如果 testA() 和 testB() 的事務傳播特性為 NOT_SUPPORTED,那么 testB() 將以非事務方式運行,并掛起當前的事務。
默認傳播特性的情況下 testB() 異常事務加入會導致 testA() 回滾,而掛起的意思是說,testB() 其內(nèi)部一旦拋出異常,不會影響 testMerge() 中其他 testA() 方法的回滾。
@Component
@RequiredArgsConstructor
@Slf4j
@Service
public class TestMergeService {
private final TestBService testBService;
private final TestAService testAService;
@Transactional
public String testMerge() {
testAService.testA();
testBService.testB();
return "ok";
}
}
@Transactional
public String testA() {
log.info("testA");
DeepzeroStandardBak entity = buildEntity();
standardBakService.save(entity);
return "ok";
}
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public String testB() {
log.info("testB");
DeepzeroStandardBak2 entity2 = buildEntity2();
standardBak2Service.save(entity2);
throw new RuntimeException("testB");
}
SUPPORTS
如果當前方法的事務傳播特性是 SUPPORTS,那么只有在調(diào)用該方法的上層方法開啟了事務的情況下,該方法的事務才會有效。如果上層方法沒有開啟事務,那么該方法的事務特性將無效。
例如,如果入口方法 testMerge() 沒有開啟事務,而 testMerge() 調(diào)用的方法 testA() 和 testB() 的事務傳播特性為 SUPPORTS,那么由于 testMerge() 沒有事務,testA() 和 testB() 將以非事務方式執(zhí)行。即使在這些方法上加上 @Transactional 注解,也不會回滾異常。
@Component
@RequiredArgsConstructor
@Slf4j
@Service
public class TestMergeService {
private final TestBService testBService;
private final TestAService testAService;
public String testMerge() {
testAService.testA();
testBService.testB();
return "ok";
}
}
@Transactional(propagation = Propagation.SUPPORTS)
public String testA() {
log.info("testA");
DeepzeroStandardBak entity = buildEntity();
standardBakService.save(entity);
return "ok";
}
@Transactional(propagation = Propagation.SUPPORTS)
public String testB() {
log.info("testB");
DeepzeroStandardBak2 entity2 = buildEntity2();
standardBak2Service.save(entity2);
throw new RuntimeException("testB");
}
10. 自己吞了異常
在整個 review 的過程中我發(fā)現(xiàn)導致事務不回滾的場景,多數(shù)是開發(fā)同學在業(yè)務代碼中手動 try...catch 捕獲了異常,然后又沒拋出異常....
比如:testMerge() 方法開啟了事務,并調(diào)用了非事務方法 testA() 和 testB(),同時在 testMerge() 中捕獲了異常。如果 testB() 中發(fā)生了異常并拋出,但 testMerge() 捕獲了這個異常而沒有繼續(xù)拋出,Spring 事務將無法捕獲到異常,從而無法進行回滾。
@RequiredArgsConstructor
@Slf4j
@Service
public class TestMergeService {
private final TestBService testBService;
private final TestAService testAService;
@Transactional
public String testMerge() {
try {
testAService.testA();
testBService.testB();
} catch (Exception e) {
log.error("testMerge error:{}", e);
}
return "ok";
}
}
@Service
public class TestAService {
public String testA() {
standardBakService.save(entity);
return "ok";
}
}
@Service
public class TestBService {
public String testB() {
standardBakService.save(entity2);
throw new RuntimeException("test2");
}
}
為了確保 Spring 事務能夠正常回滾,需要我們在 catch 塊中主動重新拋出它能夠處理的 RuntimeException 或者 Error 類型的異常。
@Transactional
public String testMerge() {
try {
testAService.testA();
testBService.testB();
} catch (Exception e) {
log.error("testMerge error:{}", e);
throw new RuntimeException(e);
}
return "ok";
}
捕獲異常并不意味著一定不會回滾,這取決于具體情況。
例如,當 testB() 方法上也加上了 @Transactional 注解時,如果在該方法中發(fā)生異常,事務會捕獲到這個異常。由于事務傳播的特性,testB() 的事務會合并到上層方法的事務中。因此,即使在 testMerge() 中捕獲了異常而未拋出,事務仍然可以成功回滾。
@Transactional
public String testB() {
DeepzeroStandardBak2 entity2 = buildEntity2();
dataImportJob2Service.save(entity2);
throw new RuntimeException("test2");
// return "ok";
}
但這有個提前,必須在 testMerge() 方法上添加 @Transactional 注解以啟用事務。如果 testMerge() 方法沒有開啟事務,不論其內(nèi)部是否使用 try 塊,都只能部分回滾 testB(),而 testA() 將無法回滾。
11. 事務無法捕獲的異常
Spring 的事務默認會回滾 RuntimeException 及其子類,以及 Error 類型的異常。
如果拋出的是其他類型的異常,例如 checked exceptions(檢查型異常),即繼承自 Exception 但不繼承自 RuntimeException 的異常,比如 SQLException、DuplicateKeyException,事務將不會回滾。
所以,我們在主動拋出異常時,要確保該異常是事務能夠捕獲的類型。
@Transactional
public String testMerge() throws Exception {
try {
testAService.testA();
testBService.testB();
} catch (Exception e) {
log.error("testMerge error:{}", e);
// throw new RuntimeException(e);
throw new Exception(e);
}
return "ok";
}
如果你非要拋出默認情況下不會導致事務回滾的異常,務必要在 @Transactional 注解的 rollbackFor 參數(shù)中明確指定該異常,這樣才能進行回滾。
@Transactional(rollbackFor = Exception.class)
public String testMerge() throws Exception {
try {
testAService.testA();
testBService.testB();
} catch (Exception e) {
log.error("testMerge error:{}", e);
// throw new RuntimeException(e);
throw new Exception(e);
}
return "ok";
}
問問你身邊的同學,哪些異常屬于運行時異常,哪些屬于檢查型異常,十有八九他們可能無法給出準確的回答!
所以減少出現(xiàn) bug 的風險,我建議使用 @Transactional 注解時,將 rollbackFor 參數(shù)設置為 Exception 或 Throwable,這樣可以擴大事務回滾的范圍。
12. 自定義異常范圍問題
針對不同業(yè)務定制異常類型是比較常見的做法,@Transactional 注解的 rollbackFor 參數(shù)支持自定義的異常,但我們往往習慣于將這些自定義異常繼承自 RuntimeException。
那么這就出現(xiàn)和上邊同樣的問題,事務的范圍不足,許多異常類型仍然無法觸發(fā)事務回滾。
@Transactional(rollbackFor = CustomException.class)
public String testMerge() throws Exception {
try {
testAService.testA();
testBService.testB();
} catch (Exception e) {
log.error("testMerge error:{}", e);
// throw new RuntimeException(e);
throw new Exception(e);
}
return "ok";
}
想要解決這個問題,可以在 catch 中主動拋出我們自定義的異常。
@Transactional(rollbackFor = CustomException.class)
public String testMerge() throws Exception {
try {
testAService.testA();
testBService.testB();
} catch (Exception e) {
log.error("testMerge error:{}", e);
throw new CustomException(e);
}
return "ok";
}
13. 嵌套事務問題
還有一種場景就是嵌套事務問題,比如,我們在 testMerge() 方法中調(diào)用了事務方法 testA() 和事務方法 testB(),此時不希望 testB() 拋出異常讓整個 testMerge() 都跟著回滾;這就需要單獨 try catch 處理 testB() 的異常,不讓異常在向上拋。
@RequiredArgsConstructor
@Slf4j
@Service
public class TestMergeService {
private final TestBService testBService;
private final TestAService testAService;
@Transactional
public String testMerge() {
testAService.testA();
try {
testBService.testB();
} catch (Exception e) {
log.error("testMerge error:{}", e);
}
return "ok";
}
}
@Service
public class TestAService {
@Transactional
public String testA() {
standardBakService.save(entity);
return "ok";
}
}
@Service
public class TestBService {
@Transactional
public String testB() {
standardBakService.save(entity2);
throw new RuntimeException("test2");
}
}
總結(jié)
上面的關于 @Transactional 注解的使用注意事項是我在代碼審查和搜集網(wǎng)絡觀點后整理出的。