Spring事务失效场景的详细整理

网友投稿 241 2022-08-28


Spring事务失效场景的详细整理

目录前言数据库引擎不支持事物方法不是 public 的自身调用问题不支持事物异常被吃掉异常类型错误总结

前言

项目中用Spring的 @Transactional 注解控制事务,使用中时常出现事物不生效的场景,本文仅限于日常项目开发中的点滴整理总结,总结以下几点,以备后续参考排查;可能不全,列举出来希望可以帮助有需要的同学,避免踩坑。

数据库引擎不支持事物

这里以 mysql 为例,其 MyISAM 引擎是不支持事务操作的,InnoDB 才是支持事务的引擎,一般要支持事务都会使用 InnoDB。

根据 MySQL 的官方文档:

https://dev.mysql.com/doc/refman/5.5/en/storage-engine-setting.html

从 MySQL 5.5.5 开始的默认存储引擎是:InnoDB,之前默认的都是:MyISAM,所以这点要值得注意,底层引擎不支持事务是硬伤。

没有被 Spring 管理

// @Service (此注解不能去掉)

public class AccountServiceImpl implements AccountService {

@Transactional

public void inser(Account account) {

// insert account

}

}

如果此时把 @Service 注解注释掉,这个类就不会被加载成一个 Bean,那这个类就不会被 Spring 管理了,事务自然就失效了。

方法不是 public 的

以下来自于Spring 官方文档:

When using proxies, you should apply the @Transactional annotation only to methods with public visibility. If you do annotate protected, private or package-visible methods with the @Transactional annotation, no error is raised, but the annotated method does not exhibit the configured transactional settings. Consider the use of AspectJ (see below) if you need to annotate non-public methods.

意思就是 @Transactional 只能用于 public 的方法上,否则事务不会失效,如果要用在非 public 方法上,可以考虑开启 AspectJ 代理模式。

自身调用问题

看下面代码

@Service

public class AccountServiceImpl implements AccountService {

public void insert(Account account) {

insertAccount(account);

}

@Transactional

public void insertAccount(Account account) {

// insert account

}

}

insert方法上面没有加 @Transactional 注解,调用有 @Transactional 注解的 insertAccount 方法,insertAccount 方法上的事务其实是不管用的。

再看下面的代码

@Service

public class AccountServiceImpl implements AccountService {

@Transactional

public void insert(Account account) {

insertAccount(account);

}

@Transactional(propagation = Propagation.REQUIhttp://RES_NEW)

public void insertAccount(Account account) {

// insert account

}

}

这次在 insert 方法上加了 @Transactional,insertAccount 加了 REQUIRES_NEW 新开启一个事务,那么新开的事务管用么?

这两个例子的答案是:不管用!

因为它们发生了自身调用,就调该类自己的方法,而没有经过 Spring 的代理类,默认只有在外部调用事务才会生效,这也是老生常谈的经典问题了。

数据源没有配置事物管理器

@Bean

public PlatformTransactionManager transactionManager(DataSource dataSource)

{

return new Datahttp://SourceTransactionManager(dataSource);

}

如上面所示,当前数据源若没有配置事务管理器,照样会失效!

不支持事物

@Service

public class AccountServiceImpl implements AccountService {

@Transactional

public void insert(Account account) {

insertAccount(account);

}

@Transactional(propagation = Propagation.NOT_SUPPORTED)

public void insertAccount(Account account) {

// insert account

}

}

Propagation.NOT_SUPPORTED: 表示不以事务运行,当前若存在事务则挂起,详细的可以参考InnoDB的事务隔离级别和传播机制。

都主动不支持以事务方式运行了,那事务生效也是白搭!

异常被吃掉

这个是比较常见的场景

@Service (此注解不能去掉)

public class AccountServiceImpl implements AccountService {

@Transactional

public void inser(Account account) {

try {

// insert account

} catch {

}

}

}

把异常吃了,然后又不抛出来,事务就无法回滚!

异常类型错误

@Service (此注解不能去掉)

public class AccountServiceImpl implements AccountService {

@Transactional

public void inser(Account account) {

try {

// insert account

} catch {

throw new Exception("新增错误");

}

}

}

这样事务也是不生效的,因为默认回滚的是:RuntimeException,如果你想触发其他异常的回滚,需要在注解上配置一下,如:

@Transactional(rollbackFor = Exception.class)

这个配置仅限于 Throwable 异常类及其子类。

查阅资料,其他失效的场景需注意:1) 像文件导入数据库,用多线程控制;可参考查询spring 多线程事务http://的问题 2)SpringBoot+Shiro引起事务失效

总结

本文总结了几种事务失效的场景,其实发生最多就是自身调用、异常被吃、异常抛出类型不对这三个了;像文章开头说的那样,本文不一定总结得全,只是根据经验总结常见的事务失效的场景。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:pytest之setup和teardown(pytest for)
下一篇:pytest标记测试用例为预期失败(@pytest.mark.xfail)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~