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