SpringBoot异常处理事务回滚(自动回滚、手动回滚、部分回滚)

1、自动回滚(不try/catch)

@Transactional(rollbackFor = Exception.class)

2、如果需要回滚全部操作,直接手工回滚异常异常即可(针对try-catch)

		@Transactional(rollbackFor = Exception.class)
		.........
        //添加表
        List<FundInfoBO> fundInfoBOList = this.insertAccrueInteRel(fundInfoBOS);
        // 构造请求参数
        InterestBaseDTO interestBaseDTO = this.assemblyInterestReqData(fundInfoBOList);
        BaseRequestDTO<Object> interestReqBaseDTO = RequestBuild.createBaseRequest(interestBaseDTO);
        try {
            //调用接口
            BaseResponseDTO<FinanceSendResp> financeResponseDTO = financeJsxbFeign.syncInterestRateInfo(interestReqBaseDTO);
            FinanceSendResp financeSendResp = financeResponseDTO.getPayload();
            //处理响应信息
            this.dealFinanceRespInfo(financeSendResp);
        } catch (MsdpBusinessException e) {
            //手工回滚异常
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseBuild.buildFailResponse(e.getCode(), e.getMessage());
        } finally {
            // 获取响应信息
            if (Objects.nonNull(interestReqBaseDTO)) {
                // 记录接口调用信息
                interfaceCallService.insertRecord(DgmsConst.YING_JI_LI_XI, DgmsDiConst.SYS_DGMS_FUND,
                        interestReqBaseDTO, financeResponseDTO1);
            }
        }

3、如果只要回滚【添加表】的操作而不回滚【记录接口调用信息】操作,可以设置事务回滚点并手工回滚异常(针对try-catch并实现部分回滚)

		@Transactional(rollbackFor = Exception.class)
		.........
		//设置事务回滚点
        Object savePoint = TransactionAspectSupport.currentTransactionStatus().createSavepoint();
        //添加表
        List<FundInfoBO> fundInfoBOList = this.insertAccrueInteRel(fundInfoBOS);
        // 构造请求参数
        InterestBaseDTO interestBaseDTO = this.assemblyInterestReqData(fundInfoBOList);
        BaseRequestDTO<Object> interestReqBaseDTO = RequestBuild.createBaseRequest(interestBaseDTO);
        try {
            //调用接口
            BaseResponseDTO<FinanceSendResp> financeResponseDTO = financeJsxbFeign.syncInterestRateInfo(interestReqBaseDTO);
            FinanceSendResp financeSendResp = financeResponseDTO.getPayload();
            //处理响应信息
            this.dealFinanceRespInfo(financeSendResp);
        } catch (MsdpBusinessException e) {
            //手工回滚异常
            TransactionAspectSupport.currentTransactionStatus().rollbackToSavepoint(savePoint);
            return ResponseBuild.buildFailResponse(e.getCode(), e.getMessage());
        } finally {
            // 获取响应信息
            if (Objects.nonNull(interestReqBaseDTO)) {
                // 记录接口调用信息
                interfaceCallService.insertRecord(DgmsConst.YING_JI_LI_XI, DgmsDiConst.SYS_DGMS_FUND,
                        interestReqBaseDTO, financeResponseDTO1);
            }
        }

你可能感兴趣的:(java知识点,java,开发语言,事务)