SpringBoot 系列教程之事务不生效的几种 case
前面几篇博文介绍了声明式事务@Transactional
的使用姿势,只知道正确的使用姿势可能还不够,还得知道什么场景下不生效,避免采坑。本文将主要介绍让事务不生效的几种 case
<!-- more -->
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
- 数据库不支持事务
- 注解放在了私有方法上
- 类内部调用
- 未捕获异常
- 多线程场景
- 传播属性设置问题
III. 其他
0. 系列博文&源码
系列博文
- 180926-SpringBoot 高级篇 DB 之基本使用
- 190407-SpringBoot 高级篇 JdbcTemplate 之数据插入使用姿势详解
- 190412-SpringBoot 高级篇 JdbcTemplate 之数据查询上篇
- 190417-SpringBoot 高级篇 JdbcTemplate 之数据查询下篇
- 190418-SpringBoot 高级篇 JdbcTemplate 之数据更新与删除
- 200119-SpringBoot 系列教程之声明式事务 Transactional
- 200120-SpringBoot 系列教程之事务隔离级别知识点小结
- 200202-SpringBoot 系列教程之事务传递属性
源码
- 工程:https://github.com/liuyueyi/spring-boot-demo
- 实例源码: https://github.com/liuyueyi/spring-boot-demo/blob/master/spring-boot/101-jdbctemplate-transaction
1. 一灰灰 Blog
尽信书则不如,以上内容,纯属一家之言,因个人能力有限,难免有疏漏和错误之处,如发现 bug 或者有更好的建议,欢迎批评指正,不吝感激
下面一灰灰的个人博客,记录所有学习和工作中的博文,欢迎大家前去逛逛
- 一灰灰 Blog 个人博客 https://blog.hhui.top
- 一灰灰 Blog-Spring 专题博客 http://spring.hhui.top

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
微服务那些事
欲速则不达,欲达则欲速! 一、前言 微服务架构被提出很短的时间内,就被越来越多的开发人员推崇,简单来说其主要的目的是有效的拆分应用,实现敏捷开发和部署。本博客尝试介绍微服务架构的一些实施细节和要求,探询微服务架构的由来,并最终提供我们团队内部的有一些实践总结,希望对大家有帮助。 二、什么是微服务 传统的web开发方式,通过对比比较容易理解什么是Microservice Architecture。和Microservice相对应的,这种方式一般被称为Monolithic(比较难传神的翻译)。所有的功能打包在一个 WAR包里,基本没有外部依赖(除了容器),部署在一个JEE容器(Tomcat,JBoss,WebLogic)里,包含了 DO/DAO,Service,UI等所有逻辑。 用《The art of scalability》一书里提到的scale cube比较容易理解如何拆分。 我们叫分库分表,为人总结成了scale cube,这就是抽象的能力,把复杂的东西用最简单的概念解释和总结。X轴代表运行多个负载均衡器之后运行的实例,Y轴代表应用进一步分解为微服务(分库),数据量大时,还可以用Z...
-
下一篇
CopyQ 3.10.0 发布,剪切板管理工具
CopyQ 是一款开源的QT编写的剪切板管理工具。支持搜索,编辑历史还有一些高级功能,支持图片,命令行,快捷键等等。 3.10.0版本已经发布,更新内容如下: 使用环境变量COPYQ_DEFAULT_ICON=1来显示原始应用程序图标,而不是当前图标主题中的图标。 避免不必要地更新菜单 使用已弃用的 Qt API 删除并至少需要 Qt 5.5(这意味着不再支持 Ubuntu14.04 和 Debian 8) 使用非本机颜色和字体对话框,可修复在 Gnome/Gtk 中显示这些内容的问题 使用分数缩放修复 GUI 修复更新托盘菜单(删除空白部分) 修复编辑同步文件内容而不是其路径的问题 OSX:省略阻止系统进入睡眠模式 X11:改进剪贴板/选择同步 X11:避免在监视过程中并行读取剪贴板 发布说明:https://github.com/hluk/CopyQ/releases
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- MySQL数据库在高并发下的优化方案
- SpringBoot2全家桶,快速入门学习开发网站教程
- CentOS8编译安装MySQL8.0.19
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- MySQL8.0.19开启GTID主从同步CentOS8
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- Dcoker安装(在线仓库),最新的服务器搭配容器使用
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- Docker容器配置,解决镜像无法拉取问题