SpringBoot 系列教程之事务不生效的几种 case

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: 前面几篇博文介绍了声明式事务@Transactional的使用姿势,只知道正确的使用姿势可能还不够,还得知道什么场景下不生效,避免采坑。本文将主要介绍让事务不生效的几种 case

image.png


前面几篇博文介绍了声明式事务@Transactional的使用姿势,只知道正确的使用姿势可能还不够,还得知道什么场景下不生效,避免采坑。本文将主要介绍让事务不生效的几种 case


I. 配置



本文的 case,将使用声明式事务,首先我们创建一个 SpringBoot 项目,版本为2.2.1.RELEASE,使用 mysql 作为目标数据库,存储引擎选择Innodb,事务隔离级别为 RR


1. 项目配置


在项目pom.xml文件中,加上spring-boot-starter-jdbc,会注入一个DataSourceTransactionManager的 bean,提供了事务支持


<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
复制代码


2. 数据库配置


进入 spring 配置文件application.properties,设置一下 db 相关的信息


## DataSource
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/story?useUnicode=true&characterEncoding=UTF-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=
复制代码


3. 数据库


新建一个简单的表结构,用于测试


CREATE TABLE `money` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(20) NOT NULL DEFAULT '' COMMENT '用户名',
  `money` int(26) NOT NULL DEFAULT '0' COMMENT '钱',
  `is_deleted` tinyint(1) NOT NULL DEFAULT '0',
  `create_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=551 DEFAULT CHARSET=utf8mb4;
复制代码


II. 不生效 case



在声明式事务的使用教程200119-SpringBoot 系列教程之声明式事务 Transactional 中,也提到了一些事务不生效的方式,比如声明式事务注解@Transactional主要是结合代理实现,结合 AOP 的知识点,至少可以得出放在私有方法上,类内部调用都不会生效,下面进入详细说明


1. 数据库


事务生效的前提是你的数据源得支持事务,比如 mysql 的 MyISAM 引擎就不支持事务,而 Innodb 支持事务


下面的 case 都是基于 mysql + Innodb 引擎


为后续的演示 case,我们准备一些数据如下

@Service
public class NotEffectDemo {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @PostConstruct
    public void init() {
        String sql = "replace into money (id, name, money) values" + " (520, '初始化', 200)," + "(530, '初始化', 200)," +
                "(540, '初始化', 200)," + "(550, '初始化', 200)";
        jdbcTemplate.execute(sql);
    }
}
复制代码


2. 类内部访问


简单来讲就是指非直接访问带注解标记的方法 B,而是通过类普通方法 A,然后由 A 访问 B


下面是一个简单的 case

/**
 * 非直接调用,不生效
 *
 * @param id
 * @return
 * @throws Exception
 */
@Transactional(rollbackFor = Exception.class)
public boolean testCompileException2(int id) throws Exception {
    if (this.updateName(id)) {
        this.query("after update name", id);
        if (this.update(id)) {
            return true;
        }
    }
    throw new Exception("参数异常");
}
public boolean testCall(int id) throws Exception {
    return testCompileException2(id);
}
复制代码


上面两个方法,直接调用testCompleException方法,事务正常操作;通过调用testCall间接访问,在不生效


测试 case 如下:

@Component
public class NotEffectSample {
    @Autowired
    private NotEffectDemo notEffectDemo;
    public void testNotEffect() {
        testCall(530, (id) -> notEffectDemo.testCall(530));
    }
    private void testCall(int id, CallFunc<Integer, Boolean> func) {
        System.out.println("============ 事务不生效case start ========== ");
        notEffectDemo.query("transaction before", id);
        try {
            // 事务可以正常工作
            func.apply(id);
        } catch (Exception e) {
        }
        notEffectDemo.query("transaction end", id);
        System.out.println("============ 事务不生效case end ========== \n");
    }
    @FunctionalInterface
    public interface CallFunc<T, R> {
        R apply(T t) throws Exception;
    }
}
复制代码


输出结果如下:

============ 事务不生效case start ==========
transaction before >>>> {id=530, name=初始化, money=200, is_deleted=false, create_at=2020-02-03 13:44:11.0, update_at=2020-02-03 13:44:11.0}
after update name >>>> {id=530, name=更新, money=200, is_deleted=false, create_at=2020-02-03 13:44:11.0, update_at=2020-02-03 13:44:11.0}
transaction end >>>> {id=530, name=更新, money=210, is_deleted=false, create_at=2020-02-03 13:44:11.0, update_at=2020-02-03 13:44:11.0}
============ 事务不生效case end ==========
复制代码


从上面的输出可以看到,事务并没有回滚,主要是因为类内部调用,不会通过代理方式访问


3. 私有方法


在私有方法上,添加@Transactional注解也不会生效,私有方法外部不能访问,所以只能内部访问,上面的 case 不生效,这个当然也不生效了


/**
 * 私有方法上的注解,不生效
 *
 * @param id
 * @return
 * @throws Exception
 */
@Transactional
private boolean testSpecialException(int id) throws Exception {
    if (this.updateName(id)) {
        this.query("after update name", id);
        if (this.update(id)) {
            return true;
        }
    }
    throw new Exception("参数异常");
}
复制代码


直接使用时,下面这种场景不太容易出现,因为 IDEA 会有提醒,文案为: Methods annotated with '@Transactional' must be overridable


4. 异常不匹配


@Transactional注解默认处理运行时异常,即只有抛出运行时异常时,才会触发事务回滚,否则并不会如


/**
 * 非运行异常,且没有通过 rollbackFor 指定抛出的异常,不生效
 *
 * @param id
 * @return
 * @throws Exception
 */
@Transactional
public boolean testCompleException(int id) throws Exception {
    if (this.updateName(id)) {
        this.query("after update name", id);
        if (this.update(id)) {
            return true;
        }
    }
    throw new Exception("参数异常");
}
复制代码


测试 case 如下


public void testNotEffect() {
    testCall(520, (id) -> notEffectDemo.testCompleException(520));
}
复制代码


输出结果如下,事务并未回滚(如果需要解决这个问题,通过设置@Transactional的 rollbackFor 属性即可)


============ 事务不生效case start ==========
transaction before >>>> {id=520, name=初始化, money=200, is_deleted=false, create_at=2020-02-03 13:44:11.0, update_at=2020-02-03 13:44:11.0}
after update name >>>> {id=520, name=更新, money=200, is_deleted=false, create_at=2020-02-03 13:44:11.0, update_at=2020-02-03 13:44:11.0}
transaction end >>>> {id=520, name=更新, money=210, is_deleted=false, create_at=2020-02-03 13:44:11.0, update_at=2020-02-03 13:44:11.0}
============ 事务不生效case end ==========
复制代码


5. 多线程


这个场景可能并不多见,在标记事务的方法内部,另起子线程执行 db 操作,此时事务同样不会生效


下面给出两个不同的姿势,一个是子线程抛异常,主线程 ok;一个是子线程 ok,主线程抛异常


a. case1


/**
 * 子线程抛异常,主线程无法捕获,导致事务不生效
 *
 * @param id
 * @return
 */
@Transactional(rollbackFor = Exception.class)
public boolean testMultThread(int id) throws InterruptedException {
    new Thread(new Runnable() {
        @Override
        public void run() {
            updateName(id);
            query("after update name", id);
        }
    }).start();
    new Thread(new Runnable() {
        @Override
        public void run() {
            boolean ans = update(id);
            query("after update id", id);
            if (!ans) {
                throw new RuntimeException("failed to update ans");
            }
        }
    }).start();
    Thread.sleep(1000);
    System.out.println("------- 子线程 --------");
    return true;
}
复制代码


上面这种场景不生效很好理解,子线程的异常不会被外部的线程捕获,testMultThread这个方法的调用不抛异常,因此不会触发事务回滚


public void testNotEffect() {
    testCall(540, (id) -> notEffectDemo.testMultThread(540));
}
复制代码


输出结果如下


============ 事务不生效case start ==========
transaction before >>>> {id=540, name=初始化, money=200, is_deleted=false, create_at=2020-02-03 13:44:11.0, update_at=2020-02-03 13:44:11.0}
after update name >>>> {id=540, name=更新, money=200, is_deleted=false, create_at=2020-02-03 13:44:11.0, update_at=2020-02-03 13:44:11.0}
Exception in thread "Thread-3" java.lang.RuntimeException: failed to update ans
  at com.git.hui.boot.jdbc.demo.NotEffectDemo$2.run(NotEffectDemo.java:112)
  at java.lang.Thread.run(Thread.java:748)
after update id >>>> {id=540, name=更新, money=210, is_deleted=false, create_at=2020-02-03 13:44:11.0, update_at=2020-02-03 13:44:11.0}
------- 子线程 --------
transaction end >>>> {id=540, name=更新, money=210, is_deleted=false, create_at=2020-02-03 13:44:11.0, update_at=2020-02-03 13:44:11.0}
============ 事务不生效case end ==========
复制代码


b. case2


/**
 * 子线程抛异常,主线程无法捕获,导致事务不生效
 *
 * @param id
 * @return
 */
@Transactional(rollbackFor = Exception.class)
public boolean testMultThread2(int id) throws InterruptedException {
    new Thread(new Runnable() {
        @Override
        public void run() {
            updateName(id);
            query("after update name", id);
        }
    }).start();
    new Thread(new Runnable() {
        @Override
        public void run() {
            boolean ans = update(id);
            query("after update id", id);
        }
    }).start();
    Thread.sleep(1000);
    System.out.println("------- 子线程 --------");
    update(id);
    query("after outer update id", id);
    throw new RuntimeException("failed to update ans");
}
复制代码


上面这个看着好像没有毛病,抛出线程,事务回滚,可惜两个子线程的修改并不会被回滚


测试代码

public void testNotEffect() {
    testCall(550, (id) -> notEffectDemo.testMultThread2(550));
}
复制代码


从下面的输出也可以知道,子线程的修改并不在同一个事务内,不会被回滚

============ 事务不生效case start ==========
transaction before >>>> {id=550, name=初始化, money=200, is_deleted=false, create_at=2020-02-03 13:52:38.0, update_at=2020-02-03 13:52:38.0}
after update name >>>> {id=550, name=更新, money=200, is_deleted=false, create_at=2020-02-03 13:52:38.0, update_at=2020-02-03 13:52:40.0}
after update id >>>> {id=550, name=更新, money=210, is_deleted=false, create_at=2020-02-03 13:52:38.0, update_at=2020-02-03 13:52:40.0}
------- 子线程 --------
after outer update id >>>> {id=550, name=更新, money=220, is_deleted=false, create_at=2020-02-03 13:52:38.0, update_at=2020-02-03 13:52:41.0}
transaction end >>>> {id=550, name=更新, money=210, is_deleted=false, create_at=2020-02-03 13:52:38.0, update_at=2020-02-03 13:52:40.0}
============ 事务不生效case end ==========
复制代码


6. 传播属性


上一篇关于传播属性的博文中,介绍了其中有几种是不走事务执行的,所以也需要额外注意下,详情可以参考博文 200202-SpringBoot 系列教程之事务传递属性


7. 小结


下面小结几种@Transactional注解事务不生效的 case


  • 数据库不支持事务
  • 注解放在了私有方法上
  • 类内部调用
  • 未捕获异常
  • 多线程场景
  • 传播属性设置问题



相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
前端开发 Java Linux
《Linux篇》02.超详细SpringBoot项目部署教程(附脚本自动部署)(三)
《Linux篇》02.超详细SpringBoot项目部署教程(附脚本自动部署)(三)
1416 0
《Linux篇》02.超详细SpringBoot项目部署教程(附脚本自动部署)(三)
|
NoSQL 数据可视化 关系型数据库
SpringBoot 多模块项目打包部署保姆级教程
SpringBoot 多模块项目打包部署保姆级教程
1044 0
SpringBoot 多模块项目打包部署保姆级教程
|
SQL Java 数据库连接
SpringBoot2.x系列教程31--SpringBoot整合H2内存数据库实现CRUD操作
前言 在上一章节中,我给大家详细讲解了H2数据库的安装配置,接下来我就带大家在Spring Boot中整合H2数据库,实现代码操作。 一. 以内嵌模式整合H2数据库 1. 创建Web项目 我们按照之前的经验,创建一个SpringBoot的Web程序,具体过程略,请参考下图创建。 2. 添加依赖包 然后在pom.xml文件中添加如下依赖包。 <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <de
1206 1
|
存储 安全 Java
SpringBoot整合SpringSecurity完整教程
SpringBoot整合SpringSecurity完整教程
SpringBoot整合SpringSecurity完整教程
|
缓存 Java 开发者
《SpringBoot篇》18.SpringBoot整合Memcached缓存超详细教程(二)
《SpringBoot篇》18.SpringBoot整合Memcached缓存超详细教程(二)
343 0
《SpringBoot篇》18.SpringBoot整合Memcached缓存超详细教程(二)
|
存储 缓存 NoSQL
《SpringBoot篇》18.SpringBoot整合Memcached缓存超详细教程(一)
《SpringBoot篇》18.SpringBoot整合Memcached缓存超详细教程(一)
166 0
《SpringBoot篇》18.SpringBoot整合Memcached缓存超详细教程(一)
|
Java 索引 容器
《SpringBoot篇》16.SpringBoot整合Elasticsearch超详细教程(三)
《SpringBoot篇》16.SpringBoot整合Elasticsearch超详细教程(三)
323 0
《SpringBoot篇》16.SpringBoot整合Elasticsearch超详细教程(三)
|
NoSQL Java MongoDB
《SpringBoot篇》16.SpringBoot整合Elasticsearch超详细教程(二)
《SpringBoot篇》16.SpringBoot整合Elasticsearch超详细教程(二)
379 0
《SpringBoot篇》16.SpringBoot整合Elasticsearch超详细教程(二)
|
存储 自然语言处理 搜索推荐
《SpringBoot篇》16.SpringBoot整合Elasticsearch超详细教程(一)
《SpringBoot篇》16.SpringBoot整合Elasticsearch超详细教程(一)
344 0
《SpringBoot篇》16.SpringBoot整合Elasticsearch超详细教程(一)
|
JSON NoSQL Java
《SpringBoot篇》15.SpringBoot整合MongoDB超详细教程(包括安装教程)(二)
《SpringBoot篇》15.SpringBoot整合MongoDB超详细教程(包括安装教程)(二)
411 0
《SpringBoot篇》15.SpringBoot整合MongoDB超详细教程(包括安装教程)(二)