SpringBoot嵌套事務(wù)詳解及失效解決方案
什么是嵌套事務(wù)?
嵌套事務(wù)是一種事務(wù)傳播行為,允許在一個事務(wù)中嵌套另一個事務(wù)。Spring 提供了 PROPAGATION_NESTED 事務(wù)傳播屬性,用于實現(xiàn)嵌套事務(wù)。嵌套事務(wù)的特點是:
- 依賴外層事務(wù):嵌套事務(wù)的提交取決于外層事務(wù)。
- 可以獨立回滾:嵌套事務(wù)失敗時,可以部分回滾,而不影響外層事務(wù)。
嵌套事務(wù)失效的原因
Spring 的事務(wù)管理基于 AOP 動態(tài)代理。當(dāng)事務(wù)方法被直接調(diào)用(例如通過 this.method())時,不經(jīng)過 Spring 的代理,事務(wù)功能會失效。
核心問題:
- 內(nèi)部方法調(diào)用不會觸發(fā) Spring 的代理邏輯。
- 因此,事務(wù)注解如 @Transactional 無法生效。
示例代碼如下:
@Service
public class ExampleService {
@Autowired
private DemoRepository demoRepository;
@Transactional
public void outerMethod() {
saveOuter();
this.innerMethod(); // 內(nèi)部調(diào)用,事務(wù)不會生效
}
private void saveOuter() {
DemoEntity entity = new DemoEntity();
entity.setName("Outer");
demoRepository.save(entity);
}
@Transactional(propagation = Propagation.NESTED)
public void innerMethod() {
DemoEntity entity = new DemoEntity();
entity.setName("Inner");
demoRepository.save(entity);
// 模擬異常
throw new RuntimeException("Inner transaction failed");
}
}在上面的代碼中,outerMethod 調(diào)用了 innerMethod,但由于是通過 this 引用調(diào)用的,事務(wù)注解 @Transactional 不會生效。
嵌套事務(wù)的解決方案
為了解決嵌套事務(wù)失效的問題,我們可以通過以下方法確保方法調(diào)用走 Spring 的代理機(jī)制。
方案一:將嵌套事務(wù)方法提取到獨立類
將 innerMethod 方法提取到一個新服務(wù)類中,確保通過 Spring 容器管理的代理類調(diào)用。
示例代碼:
- 新建一個服務(wù)類處理嵌套事務(wù):
@Service
public class InnerService {
@Autowired
private DemoRepository demoRepository;
@Transactional(propagation = Propagation.NESTED)
public void innerMethod() {
DemoEntity entity = new DemoEntity();
entity.setName("Inner");
demoRepository.save(entity);
// 模擬異常
throw new RuntimeException("Inner transaction failed");
}
}- 修改外層服務(wù)類:
@Service
public class ExampleService {
@Autowired
private InnerService innerService;
@Autowired
private DemoRepository demoRepository;
@Transactional
public void outerMethod() {
saveOuter();
innerService.innerMethod(); // 通過代理調(diào)用,事務(wù)生效
}
private void saveOuter() {
DemoEntity entity = new DemoEntity();
entity.setName("Outer");
demoRepository.save(entity);
}
}這種方式最為常見,能夠有效解決嵌套事務(wù)失效的問題。
方案二:使用 ApplicationContext 獲取代理對象
通過 Spring 的 ApplicationContext 獲取當(dāng)前類的代理對象,確保事務(wù)方法調(diào)用通過代理進(jìn)行。
示例代碼:
@Service
public class ExampleService {
@Autowired
private DemoRepository demoRepository;
@Autowired
private ApplicationContext applicationContext;
@Transactional
public void outerMethod() {
saveOuter();
// 獲取自身代理
ExampleService proxy = applicationContext.getBean(ExampleService.class);
proxy.innerMethod(); // 通過代理調(diào)用,事務(wù)生效
}
private void saveOuter() {
DemoEntity entity = new DemoEntity();
entity.setName("Outer");
demoRepository.save(entity);
}
@Transactional(propagation = Propagation.NESTED)
public void innerMethod() {
DemoEntity entity = new DemoEntity();
entity.setName("Inner");
demoRepository.save(entity);
// 模擬異常
throw new RuntimeException("Inner transaction failed");
}
}方案三:使用 AopContext 獲取代理對象
Spring 提供了 AopContext.currentProxy() 方法,可以在同一個類中獲取當(dāng)前類的代理對象。
示例代碼:
@Service
public class ExampleService {
@Autowired
private DemoRepository demoRepository;
@Transactional
public void outerMethod() {
saveOuter();
// 獲取代理對象
ExampleService proxy = (ExampleService) AopContext.currentProxy();
proxy.innerMethod(); // 通過代理調(diào)用,事務(wù)生效
}
private void saveOuter() {
DemoEntity entity = new DemoEntity();
entity.setName("Outer");
demoRepository.save(entity);
}
@Transactional(propagation = Propagation.NESTED)
public void innerMethod() {
DemoEntity entity = new DemoEntity();
entity.setName("Inner");
demoRepository.save(entity);
// 模擬異常
throw new RuntimeException("Inner transaction failed");
}
}使用該方案需要在配置中開啟 exposeProxy:
@Configuration
@EnableTransactionManagement(proxyTargetClass = true)
public class TransactionConfig {
}總結(jié)
嵌套事務(wù)在 Spring Boot 中失效的主要原因是方法調(diào)用未通過 Spring 的代理機(jī)制。我們可以通過以下方式解決:
- 將嵌套事務(wù)方法提取到獨立類(推薦方式)。
- 使用 ApplicationContext 獲取代理對象。
- 使用 AopContext 獲取代理對象。
選擇合適的解決方案可以確保嵌套事務(wù)在復(fù)雜業(yè)務(wù)場景中正常工作,避免數(shù)據(jù)一致性問題。
到此這篇關(guān)于SpringBoot嵌套事務(wù)詳解及失效解決方案的文章就介紹到這了,更多相關(guān)SpringBoot嵌套事務(wù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot項目配置多數(shù)據(jù)庫連接的示例詳解
這篇文章主要介紹了springboot項目配置多數(shù)據(jù)庫連接的示例,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-12-12
Spring Security結(jié)合JWT的方法教程
這篇文章主要給大家介紹了關(guān)于Spring Security結(jié)合JWT的方法教程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
解決@PostConstruct注解導(dǎo)致的程序無法啟動(@PostConstruct的執(zhí)行)
這篇文章主要介紹了解決@PostConstruct注解導(dǎo)致的程序無法啟動(@PostConstruct的執(zhí)行)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
SpringBoot中l(wèi)ogback日志保存到mongoDB的方法
這篇文章主要介紹了SpringBoot中l(wèi)ogback日志保存到mongoDB的方法,2017-11-11

