spring同类调用事务不生效

spring提供的声明式事务注解@Transactional,极大的方便了开发者管理事务,无需手动编写开启、提交、回滚事务的代码。

但是也带来了一些隐患,如果注解使用不当,可能导致事务不生效,最终导致脏数据也入库。

如果在同一个类直接调用事务方法,就会导致事务不生效,示例如下

public class StudentServiceImpl implements StudentService {

@Autowired

private StudentMapper studentMapper;

@Override

public void insertStudent(){

insert();

}

@Transactional(rollbackFor = Exception.class)

public void insert() {

StudentDO studentDO = new StudentDO();

studentDO.setName("小民");

studentDO.setAge(22);

studentMapper.insert(studentDO);

if (studentDO.getAge() > 18) {

throw new RuntimeException("年龄不能大于18岁");

}

}

}

事务不生效的原因在于,spring基于AOP机制实现事务的管理,@Authwired StudentService studentService这样的方式,调用StudentService的方法时,实际上是通过StudentService的代理类调用StudentService的方法,代理类在执行目标方法前后,加上了事务管理的代码。

image.png

因此,只有通过注入的StudentService调用事务方法,才会走代理类,才会执行事务管理;如果在同类直接调用,没走代理类,事务就无效。

注意:除了@Transactional,@Async同样需要代理类调用,异步才会生效

但是在实际的业务场景中,同类调用事务方法难以避免,怎么让同类调用时事务依然生效呢?有以下三个方法

方法一

自己@Autowired自己,示例如下

@Service

public class StudentServiceImpl implements StudentService {

@Autowired

private StudentMapper studentMapper;

@Autowired

private StudentService studentService;

@Override

public void insertStudent(){

studentService.insert();

}

@Override

@Transactional(rollbackFor = Exception.class)

public void insert() {

StudentDO studentDO = new StudentDO();

studentDO.setName("小民");

studentDO.setAge(22);

studentMapper.insert(studentDO);

if (studentDO.getAge() > 18) {

throw new RuntimeException("年龄不能大于18岁");

}

}

}

可能有人会担心这样会有循环依赖的问题,事实上,spring通过三级缓存解决了循环依赖的问题,所以上面的写法不会有循环依赖问题。

但是!!!,这不代表spring永远没有循环依赖的问题(@Async导致循环依赖了解下)

方法二

使用AopContext获取到当前代理类,需要在启动类加上@EnableAspectJAutoProxy(exposeProxy = true),

示例如下

@Service

public class StudentServiceImpl implements StudentService {

@Autowired

private StudentMapper studentMapper;

@Override

public void insertStudent(){

getService().insert();

}

@Override

@Transactional(rollbackFor = Exception.class)

public void insert() {

StudentDO studentDO = new StudentDO();

studentDO.setName("小民");

studentDO.setAge(22);

studentMapper.insert(studentDO);

if (studentDO.getAge() > 18) {

throw new RuntimeException("年龄不能大于18岁");

}

}

/**

* 通过AopContext获取代理类

* @return StudentService代理类

*/

private StudentService getService(){

return Objects.nonNull(AopContext.currentProxy()) ? (StudentService)AopContext.currentProxy() : this;

}

}

exposeProxy = true用于控制AOP框架公开代理,公开后才可以通过AopContext获取到当前代理类。(默认情况下不会公开代理,因为会降低性能)

注意:不能保证这种方式一定有效,使用@Async时,本方式可能失效。

(从@Async案例找到Spring框架的bug:exposeProxy=true不生效原因大剖析)

image.png

方式三(推荐)

通过spring上下文获取到当前代理类,示例如下

@Component

public class SpringBeanUtil implements ApplicationContextAware {

private static ApplicationContext applicationContext;

@Override

public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

this.applicationContext = applicationContext;

}

/**

* 通过class获取Bean

* @param clazz class

* @param 泛型

* @return bean

*/

public static T getBean(Class clazz) {

return applicationContext.getBean(clazz);

}

}

@Service

public class StudentServiceImpl implements StudentService {

@Autowired

private StudentMapper studentMapper;

@Override

public void insertStudent(){

StudentService bean = SpringBeanUtil.getBean(StudentService.class);

if (null != bean) {

bean.insert();

}

}

@Override

@Transactional(rollbackFor = Exception.class)

public void insert() {

StudentDO studentDO = new StudentDO();

studentDO.setName("小民");

studentDO.setAge(22);

studentMapper.insert(studentDO);

if (studentDO.getAge() > 18) {

throw new RuntimeException("年龄不能大于18岁");

}

}

}

当一个类实现ApplicationContextAware接口后,就可以方便的获得ApplicationContext中的所有bean。

Copyright © 2022 世界杯吉祥物_世界杯日本队 - ctpapi.com All Rights Reserved.