温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spring事务失效的场景分析

发布时间:2022-07-12 14:04:52 来源:亿速云 阅读:150 作者:iii 栏目:开发技术

这篇文章主要介绍“Spring事务失效的场景分析”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Spring事务失效的场景分析”文章能帮助大家解决问题。

1)未被Spring管理

使用Spring事务的前提是:对象要被Spring管理,事务方法所在的类要被加载为bean对象

如果事务方法所在的类没有被加载为一个bean,那么事务自然就失效了,示例:

//@Service
public class UserServiceImpl {
    @Transactional
    public void doTest() {
        // 业务代码
    }
}

2)数据库引擎不支持事务

MySQL为例,InnoDB引擎是支持事务的,而像MyISAMMEMORY等是不支持事务的。

从MySQL5.5.5开始默认的存储引擎是InnoDB,之前默认都是MyISAM。所以在开发过程中发现事务失效,不一定是Spring的锅,最好确认一下数据库表是否支持事务。

3)事务方法没有被public修饰

众所周知,java的访问权限修饰符有:privatedefaultprotectedpublic四种,

但是@Transactional注解只能作用于public修饰的方法上,

AbstractFallbackTransactionAttributeSource类(Spring通过这个类获取@Transactional注解的配置属性信息)的computeTransactionAttribute方法中有个判断,如果目标方法不是public,则TransactionAttribute返回null,即不支持事务。

@Nullable
	protected TransactionAttribute computeTransactionAttribute(Method method, @Nullable Class<?> targetClass) {
		// Don't allow no-public methods as required.
		if (allowPublicMethodsOnly() && !Modifier.isPublic(method.getModifiers())) {
			return null;
		}
        //………………
	}

其实想想动态代理的原理就很好理解了,动态代理是通过实现接口或者继承来实现的,所以目标方法必须是public修饰,并且不能是final修饰。

4)方法使用final修饰

如果一个方法不想被子类重写,那么我们就可以把他写成final修饰的方法

如果事务方法使用final修饰,那么aop就无法在代理类中重写该方法,事务就不会生效

同样的,static修饰的方法也无法通过代理变成事务方法

5)同一类中方法调用

假如在某个Service的方法中,调用了另外一个事务方法:

@Service
public class UserServiceImpl {
    @Autowired
    UserMapper userMapper;
    public void del(){
        doTest();
    }
    @Transactional
    public void doTest() {
        userMapper.deleteById(200108);
        int i = 10/0; //模拟发生异常
    }
}

像上面的代码,doTest方法使用@Transactional注解标注,在del()方法中调用了doTest()方法,在外部调用del()方法时,事务也不会生效,因为这里del()方法中调用的是类本身的方法,而不是代理对象的方法。

那么如果确实有这样的需求怎么办呢?

引入自身bean

@Service
public class UserServiceImpl {
    @Autowired
    UserMapper userMapper;
    @Autowired
    UserServiceImpl userServiceImpl;
    public void del(){
        userServiceImpl.doTest();
    }
    @Transactional
    public void doTest() {
        userMapper.deleteById(200112);
        int i = 10/0; //模拟发生异常
    }
}

通过ApplicationContext引入bean

@Service
public class UserServiceImpl {
    @Autowired
    UserMapper userMapper;
    @Autowired
    ApplicationContext applicationContext;
    public void del(){
       ((UserServiceImpl)applicationContext.getBean("userServiceImpl")).doTest();
    }
    @Transactional
    public void doTest() {
        userMapper.deleteById(200112);
        int i = 10/0; //模拟发生异常
    }
}

通过AopContext获取当前代理类

在启动类上添加注解@EnableAspectJAutoProxy(exposeProxy = true),表示是否对外暴露代理对象,即是否可以获取AopContext

@Service
public class UserServiceImpl {
    @Autowired
    UserMapper userMapper;
    @Autowired
    ApplicationContext applicationContext;
    public void del(){
        ((UserServiceImpl)AopContext.currentProxy()).doTest();
    }
    @Transactional
    public void doTest() {
        userMapper.deleteById(200112);
        int i = 10/0; //模拟发生异常
    }
}

6)未开启事务

如果是SpringBoot项目,那么SpringBoot通过DataSourceTransactionManagerAutoConfiguration自动配置类帮我们开启了事务。

如果是传统的Spring项目,则需要我们自己配置

<!--        配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource"/>
</bean>
<!--        配置事务通知-->
<tx:advice id="Advice" transaction-manager="transactionManager">
  <!--                配置事务属性,即哪些方法要执行事务-->
  <tx:attributes>
    <tx:method name="insert*" propagation="REQUIRED"/> <!-- 所有insert开头的方法,以下同理 -->
    <tx:method name="update*" propagation="REQUIRED"/>
    <tx:method name="delete*" propagation="REQUIRED"/>
  </tx:attributes>
</tx:advice>
<!--        配置事务切面-->
<aop:config>
  <aop:pointcut id="AdviceAop" expression="execution(* com.yy.service..*(..))"/> <!--要执行的方法在哪个包,意思为:com.yy.service下的所有包里面的包含任意参数的所有方法-->
  <aop:advisor advice-ref="Advice" pointcut-ref="AdviceAop"/> <!-- 配置为AdviceAop执行哪个事务通知 -->
</aop:config>

这样在执行service包下的增删改操作的方法时,就开启事务了,或者使用注解的方式

<!--        配置事务管理器-->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
                <property name="dataSource" ref="dataSource"/>
        </bean>
<!--        注解式事务声明配置-->
        <tx:annotation-driven transaction-manager="transactionManager" />

7)多线程调用

@Service
public class UserServiceImpl {
    @Autowired
    UserMapper userMapper;
    @Transactional
    public void doTest() throws InterruptedException {
        userMapper.deleteById(200110);
        new Thread(()->{
            userMapper.deleteById(200112);
            int i = 10/0; //模拟发生异常
        }).start();
    }
}

在事务方法doTest中,启动了一个新的线程,并在新的线程中发生了异常,这样doTest是不会回滚的。

因为两个操作不在一个线程中,获取到的数据库连接不一样,从而是两个不同的事务,所以也不会回滚。

8)错误的传播行为

Spring定义了7种传播行为,我们可以通propagation属性来指定传播行为参数,目前只有REQUIREDREQUIRES_NEWNESTED会创建新的事务,其他的则会以非事务的方式运行或者抛出异常

Spring事务失效的场景分析

@Service
public class UserServiceImpl {
    @Autowired
    UserMapper userMapper;
    @Transactional(propagation = Propagation.NEVER)
    public void doTest() throws InterruptedException {
        userMapper.deleteById(200114);
        int i = 10/0; //模拟发生异常
    }
}

9)自己try&hellip;catch&hellip;掉了异常

如果没有异常抛出,则Spring认为程序是正常的,就不会回滚

@Service
public class UserServiceImpl {
    @Autowired
    UserMapper userMapper;
    @Transactional
    public void doTest() {
        try{
            userMapper.deleteById(200115);
            int i = 10/0; //模拟发生异常
        }catch (Exception e){
            // 异常操作
        }
    }
}

10)手动抛出了错误的异常

Spring默认只会回滚RuntimeExceptionError对于普通的Exception,不会回滚

如果你想触发其他异常的回滚,需要在注解上配置一下,如:@Transactional(rollbackFor = Exception.class)

@Service
public class UserServiceImpl {
    @Autowired
    UserMapper userMapper;
    @Transactional
    public void doTest() throws Exception {
        try{
            userMapper.deleteById(200116);
            int i = 10/0; //模拟发生异常
        }catch (Exception e){
            // 异常操作
            throw new Exception();
        }
    }
}

11)自定义回滚异常

rollbackFor 用于指定能够触发事务回滚的异常类型,可以指定多个异常类型。

默认是在RuntimeException和Error上回滚。

若异常非配置指定的异常类,则事务失效

@Service
public class UserServiceImpl {
    @Autowired
    UserMapper userMapper;
    @Transactional(rollbackFor = NullPointerException.class)
    public void doTest() throws MyException {
        userMapper.deleteById(200118);
        throw new MyException();
    }
}

即使rollbackFor有默认值,但阿里巴巴开发者规范中,还是要求开发者重新指定该参数。

因为如果使用默认值,一旦程序抛出了Exception,事务不会回滚,这会出现很大的bug。所以,建议一般情况下,将该参数设置成:Exception或Throwable。

12)嵌套事务回滚多了

@Service
public class UserServiceImpl {
    @Autowired
    UserMapper userMapper;
    @Transactional
    public void doTest() {
        userMapper.deleteById(200118);
        ((UserServiceImpl)AopContext.currentProxy()).test02();
    }
    @Transactional(propagation = Propagation.NESTED)
    public void test02(){
        userMapper.deleteById(200119);
        int i = 10 / 0; //模拟发生异常
    }
}

test02()方法出现了异常,没有手动捕获,会继续往上抛,到外层doTest()方法的代理方法中捕获了异常。所以,这种情况是直接回滚了整个事务,不只回滚单个保存点。

如果只回滚单个保存点,可以将内部嵌套事务放在try/catch中,类似于上面的自己try&hellip;catch&hellip;掉异常,并且不继续往上抛异常。这样就能保证,如果内部嵌套事务中出现异常,只回滚内部事务,而不影响外部事务。

@Service
public class UserServiceImpl {
    @Autowired
    UserMapper userMapper;
    @Transactional
    public void doTest() {
        userMapper.deleteById(200118);
        try{
            ((UserServiceImpl)AopContext.currentProxy()).test02();
        }catch (Exception e){
            
        }
    }
    @Transactional(propagation = Propagation.NESTED)
    public void test02(){
        userMapper.deleteById(200119);
        int i = 10 / 0; //模拟发生异常
    }
}

关于“Spring事务失效的场景分析”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI