0
点赞
收藏
分享

微信扫一扫

事务失效有哪几种原因


事务失效有哪几种原因_代理类

在做业务开发时,遇到了一个事务不起作用的问题。事情是这样的,方法内部的定时任务调用了一个带事务的方法,失败后事务没有回滚。查阅资料后,问题得到解决,记录下来分享给大家。

场景

我在这里模拟一个场景,大概的调用方式就如下面的代码这样。

@Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void insertUser(User user) {
        userMapper.insertUser(user);
        throw new RuntimeException("");
    }
    
    /**
     * 内部调用新增方法
     *
     * @param user
     */
    @Override
    public void invokeInsertUser(User user) {
        this.insertUser(user);
    }

              
原因

AOP使用的是动态代理的机制,它会给类生成一个代理类,事务的相关操作都在代理类上完成。内部方式使用this调用方式时,使用的是实例调用,并没有通过代理类调用方法,所以会导致事务失效。

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

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

解决办法

方法一 引入自身bean

在类内部通过@Autowired将本身bean引入,然后通过调用自身bean,从而实现使用AOP代理操作。

注入自身bean

@Autowired
    @Lazy
    private UserService service;

修改invokeInsertUser方法

/**
     * 解决方法一 在bean中将自己注入进来
     * @param user
     */
    @Override
    public void invokeInsertUser(User user) {
        this.service.insertUser(user);
    }

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

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

方法二 通过ApplicationContext引入bean(通过spring上下文获取到当前代理类(推荐)

通过ApplicationContext获取bean,通过bean调用内部方法,就使用了bean的代理类。

注入ApplicationContext

@Autowired
    ApplicationContext applicationContext;

修改invokeInsertUser方法

/**
     * 解决方法二 通过applicationContext获取到bean
     * @param user
     */
    @Override
    public void invokeInsertUser(User user) {
        ((UserService)applicationContext.getBean("userService")).invokeInsertUser(user);
    }

demo2 整理成工具类:

@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 <T> 泛型
     * @return bean
     */
    public static <T> T getBean(Class<T> 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岁");
        }
    }
}

方法三 通过AopContext获取当前类的代理类

通过AopContext获取当前类的代理类,直接通过代理类调用方法

在引导类(启动类)上添加@EnableAspectJAutoProxy(exposeProxy=true)注解

修改invokeInsertUser方法

@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不生效原因大剖析

以上就是内部方法调用时,事务不起作用的原因及解决办法。

----------------------------------------------拓展-----------------------------------------------------------------------

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

2.spring通过三级缓存解决了循环依赖的问题

3.AopContext类详解

举报

相关推荐

0 条评论