Spring事务失效的场景分析(spring,开发技术)

时间:2024-04-30 12:54:45 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

1)未被Spring管理

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

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

//@ServicepublicclassUserServiceImpl{@TransactionalpublicvoiddoTest(){//业务代码}}

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

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

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

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

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

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

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

@Nullable protectedTransactionAttributecomputeTransactionAttribute(Methodmethod,@NullableClass<?>targetClass){ //Don'tallowno-publicmethodsasrequired. if(allowPublicMethodsOnly()&&!Modifier.isPublic(method.getModifiers())){ returnnull; }//……………… }

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

4)方法使用final修饰

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

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

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

5)同一类中方法调用

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

@ServicepublicclassUserServiceImpl{@AutowiredUserMapperuserMapper;publicvoiddel(){doTest();}@TransactionalpublicvoiddoTest(){userMapper.deleteById(200108);inti=10/0;//模拟发生异常}}

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

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

引入自身bean

@ServicepublicclassUserServiceImpl{@AutowiredUserMapperuserMapper;@AutowiredUserServiceImpluserServiceImpl;publicvoiddel(){userServiceImpl.doTest();}@TransactionalpublicvoiddoTest(){userMapper.deleteById(200112);inti=10/0;//模拟发生异常}}

通过ApplicationContext引入bean

@ServicepublicclassUserServiceImpl{@AutowiredUserMapperuserMapper;@AutowiredApplicationContextapplicationContext;publicvoiddel(){((UserServiceImpl)applicationContext.getBean("userServiceImpl")).doTest();}@TransactionalpublicvoiddoTest(){userMapper.deleteById(200112);inti=10/0;//模拟发生异常}}

通过AopContext获取当前代理类

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

@ServicepublicclassUserServiceImpl{@AutowiredUserMapperuserMapper;@AutowiredApplicationContextapplicationContext;publicvoiddel(){((UserServiceImpl)AopContext.currentProxy()).doTest();}@TransactionalpublicvoiddoTest(){userMapper.deleteById(200112);inti=10/0;//模拟发生异常}}

6)未开启事务

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

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

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

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

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

7)多线程调用

@ServicepublicclassUserServiceImpl{@AutowiredUserMapperuserMapper;@TransactionalpublicvoiddoTest()throwsInterruptedException{userMapper.deleteById(200110);newThread(()->{userMapper.deleteById(200112);inti=10/0;//模拟发生异常}).start();}}

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

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

8)错误的传播行为

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

Spring事务失效的场景分析

@ServicepublicclassUserServiceImpl{@AutowiredUserMapperuserMapper;@Transactional(propagation=Propagation.NEVER)publicvoiddoTest()throwsInterruptedException{userMapper.deleteById(200114);inti=10/0;//模拟发生异常}}

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

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

@ServicepublicclassUserServiceImpl{@AutowiredUserMapperuserMapper;@TransactionalpublicvoiddoTest(){try{userMapper.deleteById(200115);inti=10/0;//模拟发生异常}catch(Exceptione){//异常操作}}}

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

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

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

@ServicepublicclassUserServiceImpl{@AutowiredUserMapperuserMapper;@TransactionalpublicvoiddoTest()throwsException{try{userMapper.deleteById(200116);inti=10/0;//模拟发生异常}catch(Exceptione){//异常操作thrownewException();}}}

11)自定义回滚异常

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

默认是在RuntimeException和Error上回滚。

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

@ServicepublicclassUserServiceImpl{@AutowiredUserMapperuserMapper;@Transactional(rollbackFor=NullPointerException.class)publicvoiddoTest()throwsMyException{userMapper.deleteById(200118);thrownewMyException();}}

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

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

12)嵌套事务回滚多了

@ServicepublicclassUserServiceImpl{@AutowiredUserMapperuserMapper;@TransactionalpublicvoiddoTest(){userMapper.deleteById(200118);((UserServiceImpl)AopContext.currentProxy()).test02();}@Transactional(propagation=Propagation.NESTED)publicvoidtest02(){userMapper.deleteById(200119);inti=10/0;//模拟发生异常}}

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

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

@ServicepublicclassUserServiceImpl{@AutowiredUserMapperuserMapper;@TransactionalpublicvoiddoTest(){userMapper.deleteById(200118);try{((UserServiceImpl)AopContext.currentProxy()).test02();}catch(Exceptione){}}@Transactional(propagation=Propagation.NESTED)publicvoidtest02(){userMapper.deleteById(200119);inti=10/0;//模拟发生异常}}
 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:Spring事务失效的场景分析的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:JavaScript中Promise的基本概念及使用方法是什么下一篇:

7 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18