Spring中事务失效的场景

目录

异常捕获处理

原因

解决方法

抛出检查异常

原因

解决方法

非public方法

原因

解决方法


异常捕获处理

案例代码:

@Transactional
    public void update(Integer from, Integer to, Double money) {
        try {
            //转账的用户不能为空
            Account fromAccount = accountDao.selectByld(from);
            //判断用户的钱是否够转账
            if (fromAccount.getMoney() - money >= 0) {
                fromAccount.setMoney(fromAccount.getMoney() - money);
                accountDao.updateByld(fromAccount);
                //异常
                int a = 1 / 0;
                //被转账的用户
                Account toAccount = accountDao.selectByld(to);
                toAccount.setMoney(toAccount.getMoney() + money);
                accountDao.updateByld(toAccount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

原因

事务通知只有捉到了目标抛出的异常,才能进行后续的回滚处理,如果目标自己处理掉异常,事务通知无法知悉

解决方法

在catch块添加throw new RuntimeException(e)抛出

抛出检查异常

案例代码:

@Transactional
    public void update(Integer from, Integer to, Double money) throws FileNotFoundException {
        //转账的用户不能为空
        Account fromAccount = accountDao.selectByld(from);
        //判断用户的钱是否够转账
        if (fromAccount.getMoney() - money >= 0) {
            fromAccount.setMoney(fromAccount.getMoney() - money);
            accountDao.updateByld(fromAccount);

            //读取文件
            new FileInputStream("test");

            //被转账的用户
            Account toAccount = accountDao.selectByld(to);
            toAccount.setMoney(toAccount.getMoney() + money);
            accountDao.updateByld(toAccount);
        }
    }

原因

Spring 默认只会回滚非检查异常

解决方法

配置rollbackFor属性

@Transactional(rollbackFor=Exception.class)

非public方法

案例代码:

@Transactional
    void update(Integer from, Integer to, Double money) {
        //转账的用户不能为空
        Account fromAccount = accountDao.selectByld(from);
        //判断用户的钱是否够转账
        if (fromAccount.getMoney() - money >= 0) {
            fromAccount.setMoney(fromAccount.getMoney() - money);
            accountDao.updateByld(fromAccount);
            //被转账的用户
            Account toAccount = accountDao.selectByld(to);
            toAccount.setMoney(toAccount.getMoney() + money);
            accountDao.updateByld(toAccount);
        }
    }

原因

Spring为方法创建代理、添加事务通知、前提条件都是该方法是public的

解决方法

改为public方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring事务可能会失效的几个场景包括: 1. 未配置事务管理器:如果在项目没有配置Spring事务管理器,即使使用了Spring事务管理功能,事务也不会生效。[2] 2. 数据库不支持事务Spring事务的生效前提是所连接的数据库要支持事务。如果底层的数据库不支持事务,那么Spring事务肯定会失效。举个例子,如果使用的数据库为MySQL,并且选用了MyISAM存储引擎,那么Spring事务就会失效。 3. 事务方法未被Spring管理:另一个导致事务失效场景事务方法未被Spring管理。要使Spring事务生效,需要在应用程序使用@Transactional注解或配置声明式事务的XML配置,来标记需要进行事务管理的方法。如果方法未被正确标记,那么Spring将无法管理该方法的事务,从而导致事务失效。 总结起来,Spring事务可能失效场景包括未配置事务管理器、数据库不支持事务以及事务方法未被Spring管理。在使用Spring事务管理功能时,需要注意这些场景,以确保事务的正常生效。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Spring 事务失效的 8 种场景!](https://blog.csdn.net/sufu1065/article/details/122076645)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值