您现在的位置是:群英 > 开发技术 > 编程语言
Spring事务失效的情况都有哪些呢?
Admin发表于 2022-10-09 18:13:23694 次浏览
关于“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会创建新的事务,其他的则会以非事务的方式运行或者抛出异常

@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…catch…掉了异常

如果没有异常抛出,则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…catch…掉异常,并且不继续往上抛异常。这样就能保证,如果内部嵌套事务中出现异常,只回滚内部事务,而不影响外部事务。

@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事务失效的情况都有哪些呢?的内容应该都清楚了吧,希望大家阅读完这篇文章能有所收获。最后,想要了解更多Spring事务失效的情况都有哪些呢?的知识,欢迎关注群英网络,群英网络将为大家推送更多相关知识的文章。

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

标签: Spring事务失效
相关信息推荐
2022-05-11 16:42:00 
摘要:php preg_match()函数会匹配1次。preg_match()函数用于根据正则表达式对字符串进行搜索匹配,可以返回匹配次数;preg_match()的值将是0次(不匹配)或1次,因为它在第一次匹配后将会停止搜索。
2022-05-11 17:58:33 
摘要:判断方法:1、使用“$("父元素").has("子元素").length”语句,如果返回值为1,则指定子元素存在;2、使用“$("父元素").children("子元素").length”语句,如果返回值大于等于1,则指定子元素存在。
2022-07-14 17:15:11 
摘要:在php中,rewind()函数方法用于将文件指针的位置倒回文件的开头,若指针的位置倒回成功,则该函数返回的结果是true,如果指针的位置倒回不成功,则该函数的返回结果是false,语法为“rewind(规定已打开的文件)”。
群英网络助力开启安全的云计算之旅
立即注册,领取新人大礼包
  • 联系我们
  • 24小时售后:4006784567
  • 24小时TEL :0668-2555666
  • 售前咨询TEL:400-678-4567

  • 官方微信

    官方微信
Copyright  ©  QY  Network  Company  Ltd. All  Rights  Reserved. 2003-2019  群英网络  版权所有   茂名市群英网络有限公司
增值电信经营许可证 : B1.B2-20140078   粤ICP备09006778号
免费拨打  400-678-4567
免费拨打  400-678-4567 免费拨打 400-678-4567 或 0668-2555555
微信公众号
返回顶部
返回顶部 返回顶部