SpringBoot嵌套事务详解及失效解决方案

 更新时间:2025年01月02日 09:41:15   作者:silence250  
在复杂的业务场景中,嵌套事务可以帮助我们更加精细地控制数据的一致性,然而,在 Spring Boot 中,如果嵌套事务的配置不当,可能会导致事务不生效的问题,尤其是在同一个类中进行方法调用时,本文将详细介绍嵌套事务的原理、失效的原因以及解决方案

什么是嵌套事务?

嵌套事务是一种事务传播行为,允许在一个事务中嵌套另一个事务。Spring 提供了 PROPAGATION_NESTED 事务传播属性,用于实现嵌套事务。嵌套事务的特点是:

  • 依赖外层事务:嵌套事务的提交取决于外层事务。
  • 可以独立回滚:嵌套事务失败时,可以部分回滚,而不影响外层事务。

嵌套事务失效的原因

Spring 的事务管理基于 AOP 动态代理。当事务方法被直接调用(例如通过 this.method())时,不经过 Spring 的代理,事务功能会失效。

核心问题:

  • 内部方法调用不会触发 Spring 的代理逻辑。
  • 因此,事务注解如 @Transactional 无法生效。

示例代码如下:

@Service
public class ExampleService {
    @Autowired
    private DemoRepository demoRepository;
 
    @Transactional
    public void outerMethod() {
        saveOuter();
        this.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");
    }
}

在上面的代码中,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 获取代理对象

选择合适的解决方案可以确保嵌套事务在复杂业务场景中正常工作,避免数据一致性问题。

到此这篇关于SpringBoot嵌套事务详解及失效解决方案的文章就介绍到这了,更多相关SpringBoot嵌套事务内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • springboot项目配置多数据库连接的示例详解

    springboot项目配置多数据库连接的示例详解

    这篇文章主要介绍了springboot项目配置多数据库连接的示例,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2023-12-12
  • SpringBoot部署在Weblogic的操作步骤

    SpringBoot部署在Weblogic的操作步骤

    这篇文章主要介绍了SpringBoot部署在Weblogic的操作步骤,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07
  • Springboot文件上传功能简单测试

    Springboot文件上传功能简单测试

    这篇文章主要介绍了Springboot文件上传功能简单测试,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-05-05
  • SpringBoot框架底层原理解析

    SpringBoot框架底层原理解析

    这篇文章主要介绍了SpringBoot底层原理,包括配置优先级的配置方式给大家讲解的非常详细,需要的朋友可以参考下
    2024-03-03
  • Spring Security结合JWT的方法教程

    Spring Security结合JWT的方法教程

    这篇文章主要给大家介绍了关于Spring Security结合JWT的方法教程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2017-12-12
  • Maven的使用之继承与聚合

    Maven的使用之继承与聚合

    这篇文章主要为大家详细介绍了Maven的继承和聚合,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2023-04-04
  • Java使用递归解决算法问题的实例讲解

    Java使用递归解决算法问题的实例讲解

    递归算法的实质是把问题分解成规模缩小的同类问题的子问题,然后递归调用方法来表示问题的解,这里我们就来看几个Java使用递归解决算法问题的实例讲解
    2016-06-06
  • 解决@PostConstruct注解导致的程序无法启动(@PostConstruct的执行)

    解决@PostConstruct注解导致的程序无法启动(@PostConstruct的执行)

    这篇文章主要介绍了解决@PostConstruct注解导致的程序无法启动(@PostConstruct的执行)问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-01-01
  • SpringBoot中logback日志保存到mongoDB的方法

    SpringBoot中logback日志保存到mongoDB的方法

    这篇文章主要介绍了SpringBoot中logback日志保存到mongoDB的方法,
    2017-11-11
  • Java8中常见函数式接口的使用示例详解

    Java8中常见函数式接口的使用示例详解

    在 Java 8 中,函数式接口是一个关键的特性,它们允许将方法作为参数传递或返回类型,本文为大家整理了一些常见的函数式接口的使用,希望对大家有所帮助
    2023-12-12

最新评论