Spring原理解读——事务传播行为

本文深入探讨了Spring框架中的事务传播行为,包括REQUIRED、REQUIRES_NEW和NESTED等模式。通过示例代码展示了它们在事务管理中的实际应用和效果,解释了不同传播行为下内外层事务的关系及其回滚机制。了解这些概念对于优化事务处理和确保数据一致性至关重要。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring原理解读——事务传播行为


写在前面

不要使用junit等测试方法测试事务,spring会自动回滚事务

不要把不同事务写在同一个类里,这样无法打到事务嵌套的测试效果

请手动回滚,不要尝试向上层抛出异常

准备工作

maven依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

一个工具类

public class TransactionUtil {

    public static void manaul(){
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
    }

}

表结构

create table user
(
    id       bigint               not null comment '主键'
        primary key,
    username varchar(40)          null comment '用户名',
    password varchar(100)         null comment '密码',
    locked   tinyint(1) default 0 null comment '是否锁定',
    disable  tinyint(1) default 0 null comment '禁用'
);

一个Controller

@RestController
@RequestMapping("/testController")
public class TestController {

    @Autowired
    private TestService testService;

    @GetMapping("/testMethod")
    public void testMethod(){
        testService.testMethod();
    }

}

两个Service

@Service
public class TestService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private TestService2 testMethod2;

    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public void testMethod() {
        testMethod2.testMethod2();
        jdbcTemplate.update("insert into fastkdm.user (id, username, password) VALUE (2,'transaction_test','111111')");
        manaul();
    }

}
@Service
public class TestService2 {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
    public void testMethod2(){
        jdbcTemplate.update("insert into fastkdm.user (id, username, password) VALUE (3,'transaction_test2','111111')");
    }

}

1、REQUIRED

默认传播行为

使用当前的事务,如果不存在,就新建一个事务

结果

内外层属于同一事务,任意一层回滚会导致整体回滚

2、REQUIRES_NEW

内层新建一个事务

如果外层事务存在,则先将外层挂起,等内层执行完再执行外层

结果

内外层属于不同事务,层与层之间的事务互不影响

3、NESTED

内层新建一个嵌套事务,隶属于外层

如果外层事务存在,先提交外层事务,然后记录外层操作点,再执行内层,内层失败返回操作点

结果

内层事务嵌套在外层事务里,内层回滚不会影响外层,外层回滚会连同内层一起

4、MANDATORY

外层必须有事务

5、NEVER

外层必须没事务

6、SUPPORTS

内层使用的事务传播类型取决于外层,没有则不使用

7、NOT_SUPPORT

不论外层有没有事务,内层都不由事务控制

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wheat_Liu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值