记一次 @Transactional不生效的问题

今天写代码的时候有一个service需要用到事务,故使用@Transactional注解

@Transactional
Map<String, Object> joinTeam(Long teamId, Long userId) throws Exception;

这里抛出自己定义的异常来实现事务回滚

接口实现类方法如下

 public Map<String, Object> joinTeam(Long teamId, Long userId) throws Exception {
        Map<String, Object> result = new HashMap<>();
        Team team = teamService.getTeamById(teamId);
        //添加组队成员
        TeamMember member = new TeamMember();
        member.setUserId(userId);
        member.setTeamId(teamId);
        teamMemberDao.saveTeamMember(member);

        //更新组队人数
        team.setCurrentSignup(team.getCurrentSignup() + 1);
        Long count = teamService.updateTeamCurrentSignup(team);
        int i = 0;
        while (count == 0) {
            if (i >= 3) {
                throw new BaseException(BaseException.OPTIMISTIC_LOCK);
            }
            team = teamService.getTeamById(teamId);
            team.setCurrentSignup(team.getCurrentSignup() + 1);
            count = teamService.updateTeamCurrentSignup(team);
            i++;
        }

        result.put("success", true);
        result.put("message", "加入成功!");
        throw new Exception(BaseException.OPTIMISTIC_LOCK);
    }

teamMemberDao.saveTeamMember(member)count = teamService.updateTeamCurrentSignup(team) 、两个修改库操作,需要 teamService.updateTeamCurrentSignup(team) 抛异常来控制 teamMemberDao.saveTeamMember(member) 的数据回滚

但是结尾抛异常数据并不回滚,很是糟心。

于是查看Spring的Transactional的API文档,发现下面这段:

If no rules are relevant to the exception, it will be treated like DefaultTransactionAttribute (rolling back on runtime exceptions).

所以Transactional默认异常回滚是runtimeexcetion才回滚

excetion是所有异常的总称。
而runtimeexcetion是具体的某一个异常。

所以得将Transactional设置回滚异常为excetion

故将接口修改如下,这次再抛自定义异常就会回滚了

@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
Map<String, Object> joinTeam(Long teamId, Long userId) throws Exception;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值