欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

spring同类调用事务不生效-原因及三种解决方式

发布时间:2024/1/23 编程问答 47 豆豆
生活随笔 收集整理的这篇文章主要介绍了 spring同类调用事务不生效-原因及三种解决方式 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

spring提供的声明式事务注解@Transactional,极大的方便了开发者管理事务,无需手动编写开启、提交、回滚事务的代码。
但是也带来了一些隐患,如果注解使用不当,可能导致事务不生效,最终导致脏数据也入库。

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

 

public class StudentServiceImpl implements StudentService {@Autowiredprivate StudentMapper studentMapper;@Overridepublic 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 {@Autowiredprivate StudentMapper studentMapper;@Autowiredprivate StudentService studentService;@Overridepublic 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 {@Autowiredprivate StudentMapper studentMapper;@Overridepublic 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;@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {this.applicationContext = applicationContext;}/*** 通过class获取Bean* @param clazz class* @param <T> 泛型* @return bean*/public static <T> T getBean(Class<T> clazz) {return applicationContext.getBean(clazz);} }

 

@Service public class StudentServiceImpl implements StudentService {@Autowiredprivate StudentMapper studentMapper;@Overridepublic 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。



作者:修行者12138
链接:https://www.jianshu.com/p/083605986c8f
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

总结

以上是生活随笔为你收集整理的spring同类调用事务不生效-原因及三种解决方式的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。