Java外功精要(6)——Spring事务及其传播机制

1.概述

Spring事务管理是Spring框架中用于确保数据库操作 原子性、一致性、隔离性和持久性(ACID)

的核心机制。它通过声明式编程式(本文略)方式管理事务,支持多种事务传播行为和隔离级别

2.@Transactional

作用:提供声明式事务管理 。它简化了在应用程序中管理数据库事务的流程。开发者只需在方法或类上添加此注解,Spring框架就会自动处理事务的开启、提交和回滚 ,无需手动编写事务管理代码(如 begin、commit、rollback)
级别:类 + 方法

  • 作为类注解:为类中所有public方法添加注解
  • 作为方法注解:默认仅对public方法生效
java 复制代码
@RequestMapping("/test")
@RestController
@Slf4j
public class TestController {
    private final UserService userService;

    @Autowired
    public TestController(UserService userService) {
        this.userService = userService;
    }

    @Transactional
    @RequestMapping("/test1")
    public String test1(String userName,String password) {
        UserInfo userInfo = new UserInfo();
        userInfo.setUserName(userName);
        userInfo.setPassword(password);
        Integer result = userService.register(userInfo);
        if (result == 1){
            log.info("test1注册成功,userName:{},password:{}", userName, password);
        }
        return "注册成功";
    }
}

使用PostMan向后端发送请求:

MySQL查询结果如下:

后端日志日志如下:

2.1 rollbackfor

作用:指定哪些异常触发回滚,默认情况下在抛出 非受查异常(RuntimeException)/错误(Error) 时触发回滚

  • 抛出受查异常时

    java 复制代码
    @RequestMapping("/test")
    @RestController
    @Slf4j
    public class TestController {
        private final UserService userService;
    
        @Autowired
        public TestController(UserService userService) {
            this.userService = userService;
        }
    
        @Transactional
        @RequestMapping("/test2")
        public String test2(String userName,String password) throws IOException {
            UserInfo userInfo = new UserInfo();
            userInfo.setUserName(userName);
            userInfo.setPassword(password);
            Integer result = userService.register(userInfo);
            if (result == 1){
                log.info("test2注册成功,userName:{},password:{}", userName, password);
                throw new IOException();
            }
            return "注册成功";
        }
    }

    使用PostMan向后端发送请求:

    MySQL查询结果如下:

    后端日志日志如下:

  • 抛出非受查异常时

    java 复制代码
    @RequestMapping("/test")
    @RestController
    @Slf4j
    public class TestController {
        private final UserService userService;
    
        @Autowired
        public TestController(UserService userService) {
            this.userService = userService;
        }
    
        @Transactional
        @RequestMapping("/test3")
        public String test3(String userName,String password) {
            UserInfo userInfo = new UserInfo();
            userInfo.setUserName(userName);
            userInfo.setPassword(password);
            Integer result = userService.register(userInfo);
            if (result == 1){
                log.info("test3注册成功,userName:{},password:{}", userName, password);
                throw new RuntimeException();
            }
            return "注册成功";
        }
    }

    使用PostMan向后端发送请求:

    MySQL查询结果如下:

    后端日志日志如下:

  • 指定回滚类型

    java 复制代码
    @RequestMapping("/test")
    @RestController
    @Slf4j
    public class TestController {
        private final UserService userService;
    
        @Autowired
        public TestController(UserService userService) {
            this.userService = userService;
        }
    
        @Transactional(rollbackFor = Exception.class)
        @RequestMapping("/test4")
        public String test4(String userName,String password) throws IOException {
            UserInfo userInfo = new UserInfo();
            userInfo.setUserName(userName);
            userInfo.setPassword(password);
            Integer result = userService.register(userInfo);
            if (result == 1){
                log.info("test4注册成功,userName:{},password:{}", userName, password);
                throw new IOException();
            }
            return "注册成功";
        }
    }

    使用PostMan向后端发送请求:

    MySQL查询结果如下:

    后端日志日志如下:

2.2 isolation

作用:用于指定事务的隔离级别

  • Isolation.DEFAULT:使用底层数据库默认的隔离级别
  • Isolation.READ_UNCOMMITTED:读未提交
  • Isolation.READ_COMMITTED:读已提交
  • Isolation.REPEATABLE_READ:可重复读
  • Isolation.SERIALIZABLE:串行化
    每种隔离级别的具体效果在全面解析MySQL(5)------"索引、事务、JDBC"三大核心一文中有介绍,本文不再赘述

2.3 propagation

作用:用于定义事务的传播行为,即当前事务方法被另一个事务方法调用时,事务应如何传播。Spring提供了7种传播行为,均基于Propagation枚举类实现

2.3.1 Propagation.REQUIRED

默认传播行为。如果当前存在事务,则加入该事务;如果不存在事务,则新建一个事务

2.3.2 Propagation.SUPPORTS

如果当前存在事务,则加入该事务;如果不存在事务,则以非事务方式执行

2.3.3 Propagation.MANDATORY

强制要求当前存在事务并加入,否则抛出异常

2.3.4 Propagation.REQUIRES_NEW

无论当前是否存在事务,都新建一个事务。新事务与当前事务独立,互不干扰

2.3.5 Propagation.NOT_SUPPORTED

以非事务方式执行操作,如果当前存在事务,则挂起该事务

2.3.6 Propagation.NEVER

强制要求当前不能存在事务,否则抛出异常

2.3.7 Propagation.NESTED

如果当前存在事务,则在嵌套事务中执行;如果不存在事务,则行为与Propagation.REQUIRED相同。嵌套事务的回滚不影响外部事务,但外部事务回滚会导致嵌套事务回滚(适用于需要部分回滚的场景 )

3.Gitee

Gitee地址:九转苍翎

本文源码:spring-trans

相关推荐
是梦终空6 小时前
计算机毕业设计264—基于Springboot+Vue3+协同过滤的房屋租赁管理系统(源代码+数据库+万字论文+设计文档)
spring boot·毕业设计·vue3·课程设计·毕业论文·协同过滤·房屋租赁管理系统
qq_297574676 小时前
【实战】POI 实现 Excel 多级表头导出(含合并单元格完整方案)
java·spring boot·后端·excel
风流倜傥唐伯虎16 小时前
Spring Boot Jar包生产级启停脚本
java·运维·spring boot
fuquxiaoguang16 小时前
深入浅出:使用MDC构建SpringBoot全链路请求追踪系统
java·spring boot·后端·调用链分析
毕设源码_廖学姐17 小时前
计算机毕业设计springboot招聘系统网站 基于SpringBoot的在线人才对接平台 SpringBoot驱动的智能求职与招聘服务网
spring boot·后端·课程设计
顾北1217 小时前
MCP服务端开发:图片搜索助力旅游计划
java·spring boot·dubbo
昀贝17 小时前
IDEA启动SpringBoot项目时报错:命令行过长
java·spring boot·intellij-idea
indexsunny18 小时前
互联网大厂Java面试实战:Spring Boot微服务在电商场景中的应用与挑战
java·spring boot·redis·微服务·kafka·spring security·电商
Coder_Boy_19 小时前
基于SpringAI的在线考试系统-相关技术栈(分布式场景下事件机制)
java·spring boot·分布式·ddd
韩立学长1 天前
基于Springboot泉州旅游攻略平台d5h5zz02(程序、源码、数据库、调试部署方案及开发环境)系统界面展示及获取方式置于文档末尾,可供参考。
数据库·spring boot·旅游