事务手动回滚

时光怂恿深爱的人放手 提交于 2019-12-02 18:22:45
    @Override
    public BaseResponse transferStuPlan(GxyPlanTeacherStudentEntity teacherStudentEntity) {

      
        boolean  stuIHaveJob = false;
        List<GxyPlanTeacherStudentEntity> deleteStu = new ArrayList<>();
        for(String stuId:teacherStudentEntity.getStuIds()){
            GxyPlanTeacherStudentEntity teacherStudentDto = planTeacherStudentMapper.selectPlanIdAndStuId(teacherStudentEntity.getPlanId(),stuId,teacherStudentEntity.getSnowFlakeId());
         
                Integer countReport = reportService.countReport(reportEntity);
                if(countReport != null && countReport>0){
                    stuIHaveJob = true;
                    break;
                }

                planTeacherStudentMapper.delete(new QueryWrapper<GxyPlanTeacherStudentDto>().eq("plan_teacher_student_id",teacherStudentDto.getPlanTeacherStudentId()).eq("snow_flake_id",teacherStudentEntity.getSnowFlakeId()));
                deleteStu.add(teacherStudentDto);
           
        }

        if(stuIHaveJob){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            if(teacherStudentEntity.getStuIds().size()>1){
                return BaseResponse.error("无法删除");
            }
        }else{
            updatePlanIdIntoNewPlan(deleteStu,teacherStudentEntity.getNewPlanId(),teacherStudentEntity.getSnowFlakeId());
        }
        return BaseResponse.ok();
    }
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();   手动回滚。当 stuIHaveJob 为true 时,上面的 planTeacherStudentMapper.delete 会回滚。
当 stuIHaveJob 为false 时,上面的 planTeacherStudentMapper.delete 不会回滚,继续执行 updatePlanIdIntoNewPlan。(当此方法中有异常时,也会回滚)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!