欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot嵌套事務詳解及失效解決方案

 更新時間:2025年01月02日 09:41:15   作者:silence250  
在復雜的業(yè)務場景中,嵌套事務可以幫助我們更加精細地控制數(shù)據(jù)的一致性,然而,在 Spring Boot 中,如果嵌套事務的配置不當,可能會導致事務不生效的問題,尤其是在同一個類中進行方法調用時,本文將詳細介紹嵌套事務的原理、失效的原因以及解決方案

什么是嵌套事務?

嵌套事務是一種事務傳播行為,允許在一個事務中嵌套另一個事務。Spring 提供了 PROPAGATION_NESTED 事務傳播屬性,用于實現(xiàn)嵌套事務。嵌套事務的特點是:

  • 依賴外層事務:嵌套事務的提交取決于外層事務。
  • 可以獨立回滾:嵌套事務失敗時,可以部分回滾,而不影響外層事務。

嵌套事務失效的原因

Spring 的事務管理基于 AOP 動態(tài)代理。當事務方法被直接調用(例如通過 this.method())時,不經(jīng)過 Spring 的代理,事務功能會失效。

核心問題:

  • 內(nèi)部方法調用不會觸發(fā) Spring 的代理邏輯。
  • 因此,事務注解如 @Transactional 無法生效。

示例代碼如下:

@Service
public class ExampleService {
    @Autowired
    private DemoRepository demoRepository;
 
    @Transactional
    public void outerMethod() {
        saveOuter();
        this.innerMethod(); // 內(nèi)部調用,事務不會生效
    }
 
    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 調用了 innerMethod,但由于是通過 this 引用調用的,事務注解 @Transactional 不會生效。

嵌套事務的解決方案

為了解決嵌套事務失效的問題,我們可以通過以下方法確保方法調用走 Spring 的代理機制。

方案一:將嵌套事務方法提取到獨立類

將 innerMethod 方法提取到一個新服務類中,確保通過 Spring 容器管理的代理類調用。

示例代碼

  • 新建一個服務類處理嵌套事務:
@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");
    }
}
  • 修改外層服務類:
@Service
public class ExampleService {
    @Autowired
    private InnerService innerService;
 
    @Autowired
    private DemoRepository demoRepository;
 
    @Transactional
    public void outerMethod() {
        saveOuter();
        innerService.innerMethod(); // 通過代理調用,事務生效
    }
 
    private void saveOuter() {
        DemoEntity entity = new DemoEntity();
        entity.setName("Outer");
        demoRepository.save(entity);
    }
}

這種方式最為常見,能夠有效解決嵌套事務失效的問題。

方案二:使用 ApplicationContext 獲取代理對象

通過 Spring 的 ApplicationContext 獲取當前類的代理對象,確保事務方法調用通過代理進行。

示例代碼

@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(); // 通過代理調用,事務生效
    }
 
    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() 方法,可以在同一個類中獲取當前類的代理對象。

示例代碼

@Service
public class ExampleService {
    @Autowired
    private DemoRepository demoRepository;
 
    @Transactional
    public void outerMethod() {
        saveOuter();
 
        // 獲取代理對象
        ExampleService proxy = (ExampleService) AopContext.currentProxy();
        proxy.innerMethod(); // 通過代理調用,事務生效
    }
 
    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 {
}

總結

嵌套事務在 Spring Boot 中失效的主要原因是方法調用未通過 Spring 的代理機制。我們可以通過以下方式解決:

  1. 將嵌套事務方法提取到獨立類(推薦方式)。
  2. 使用 ApplicationContext 獲取代理對象。
  3. 使用 AopContext 獲取代理對象。

選擇合適的解決方案可以確保嵌套事務在復雜業(yè)務場景中正常工作,避免數(shù)據(jù)一致性問題。

到此這篇關于SpringBoot嵌套事務詳解及失效解決方案的文章就介紹到這了,更多相關SpringBoot嵌套事務內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • springboot項目配置多數(shù)據(jù)庫連接的示例詳解

    springboot項目配置多數(shù)據(jù)庫連接的示例詳解

    這篇文章主要介紹了springboot項目配置多數(shù)據(jù)庫連接的示例,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2023-12-12
  • SpringBoot部署在Weblogic的操作步驟

    SpringBoot部署在Weblogic的操作步驟

    這篇文章主要介紹了SpringBoot部署在Weblogic的操作步驟,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Springboot文件上傳功能簡單測試

    Springboot文件上傳功能簡單測試

    這篇文章主要介紹了Springboot文件上傳功能簡單測試,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-05-05
  • SpringBoot框架底層原理解析

    SpringBoot框架底層原理解析

    這篇文章主要介紹了SpringBoot底層原理,包括配置優(yōu)先級的配置方式給大家講解的非常詳細,需要的朋友可以參考下
    2024-03-03
  • Spring Security結合JWT的方法教程

    Spring Security結合JWT的方法教程

    這篇文章主要給大家介紹了關于Spring Security結合JWT的方法教程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。
    2017-12-12
  • Maven的使用之繼承與聚合

    Maven的使用之繼承與聚合

    這篇文章主要為大家詳細介紹了Maven的繼承和聚合,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2023-04-04
  • Java使用遞歸解決算法問題的實例講解

    Java使用遞歸解決算法問題的實例講解

    遞歸算法的實質是把問題分解成規(guī)??s小的同類問題的子問題,然后遞歸調用方法來表示問題的解,這里我們就來看幾個Java使用遞歸解決算法問題的實例講解
    2016-06-06
  • 解決@PostConstruct注解導致的程序無法啟動(@PostConstruct的執(zhí)行)

    解決@PostConstruct注解導致的程序無法啟動(@PostConstruct的執(zhí)行)

    這篇文章主要介紹了解決@PostConstruct注解導致的程序無法啟動(@PostConstruct的執(zhí)行)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • SpringBoot中l(wèi)ogback日志保存到mongoDB的方法

    SpringBoot中l(wèi)ogback日志保存到mongoDB的方法

    這篇文章主要介紹了SpringBoot中l(wèi)ogback日志保存到mongoDB的方法,
    2017-11-11
  • Java8中常見函數(shù)式接口的使用示例詳解

    Java8中常見函數(shù)式接口的使用示例詳解

    在 Java 8 中,函數(shù)式接口是一個關鍵的特性,它們允許將方法作為參數(shù)傳遞或返回類型,本文為大家整理了一些常見的函數(shù)式接口的使用,希望對大家有所幫助
    2023-12-12

最新評論