【问题标题】:Spring Boot: @Transactional rollbackFor checked ExceptionSpring Boot:@Transactional rollbackFor 检查异常
【发布时间】:2020-07-30 11:54:17
【问题描述】:

当我从“invokeProcedureForLead”方法(下面提到的代码)获得检查异常时,我正在尝试实现事务回滚。我尝试了多种方法,仍然有一些参考 它不适合我。

更多理解请看下面的代码

@Transactional(rollbackFor = Exception.class)
    public LeadResponseDTO processDataInDB(LMCRAResponseData lmcraResponse,
            Boolean crnPresentFlag, Map<Integer, Integer> craProcData,
            Map<Integer, Integer> crnProcData,LeadResponseDTO leadResponseDTO,LMCRARequestData lmcraRequestData) throws CRAProcessDBException,SQLException, CRAProcessClientException {
        try{
        leadResponseDTO = extractDecision(lmcraResponse,crnPresentFlag);
        
        decisionEngineResponseDao.invokeProceduresForLead(craProcData, crnProcData, lmcraResponse);
        
        }catch(Exception e){
              
        log.error("error in invokeProcCalls", e);
        if (masterErrorCodes.getErrorDTO("6006") != null)
            logException(lmcraResponse, masterErrorCodes.getErrorDTO("6006"));
         leadResponseDTO=new LeadResponseDTO(); 
         getLeadResponseDTO(lmcraRequestData,leadResponseDTO,e.getMessage());
        
    
        
        }       
    
        return leadResponseDTO;
    }



public void invokeProceduresForLead(Map<Integer, Integer> craProcData,
            Map<Integer, Integer> crnProcData, LMCRAResponseData lmcraResponseData) throws   Exception  {
        int noCRNFlag = 0;
        String commonLogs = CommonUtil.printDECommonLogs(lmcraResponseData);
    
        if (lmcraResponseData.isBureauMatch() && crnProcData.isEmpty())
            noCRNFlag = 1;

        
        if (lmcraResponseData.isBureauMatch()
                && invokeProcLeads(craProcData, DBConstants.CALL_PROC_PROCESS_CRA_DATA_LEAD,
                        Integer.parseInt(lmcraResponseData.getCaseID())) != null) {
            log.info("invokeProcLeads for Cra block - 1 for runId {} " , commonLogs);
            throw new CRAProcessDBException("error in invokeProcCalls for CRA");
            
            
        }
        if (crnProcData != null
                && !crnProcData.isEmpty()
                && invokeProcLeads(crnProcData, DBConstants.CALL_PROC_PROCESS_CRN_DATA_LEAD,
                        Integer.parseInt(lmcraResponseData.getCaseID())) != null) {
            log.info("invokeProcLeads for crn  block - 2 for runId {} " , commonLogs);
            throw new CRAProcessDBException("error in invokeProcCalls for CRN");
            
        }
        if (((crnProcData == null || crnProcData.isEmpty()) || !lmcraResponseData.isBureauMatch())
                && invokeNoCrnForLeads(DBConstants.CALL_PROC_PROCESS_NO_CRN_DATA_LEAD,
                        Integer.parseInt(lmcraResponseData.getCaseID()), noCRNFlag) != null) {
            log.info("invokeNoCrnForLeads block - 3 for runId {} " , commonLogs);
            throw new CRAProcessDBException("error in invokeProcCalls for NOCRN");
            
            
        }

【问题讨论】:

  • 你不希望有一个 caught 异常的回滚,是吗?
  • 既然processDataInDB方法没有抛出异常,那为什么还要回滚呢?
  • 方法processDataInDB中没有调用invokeProceduresForLead
  • 是的。在 processDataInDB“decisionEngineResponseDao.invokeProceduresForLead”中提到了 invokeProceduresForLead 调用,请检查“processDataInDB”中的这一行。例如,考虑有三个过程,如果第三个过程失败并抛出异常,那么它应该回滚前两个过程的执行。这就是我所期待的

标签: java spring spring-boot hibernate transactions


【解决方案1】:

我觉得代码需要改成这样-

log.error("error in invokeProcCalls", e);

if (masterErrorCodes.getErrorDTO("6006") != null)

logException(lmcraResponse, masterErrorCodes.getErrorDTO("6006"));

leadResponseDTO=新的 LeadResponseDTO(); getLeadResponseDTO(lmcraRequestData,leadResponseDTO,e.getMessage());

}

投掷;

注意 **throw e; ** 添加行..

问题是你从内部调用抛出异常,然后在外部方法中吞下它。所以外部方法永远不会抛出异常,不会发生回滚。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-06
    • 2020-12-11
    • 2020-10-18
    • 2020-12-05
    • 2020-01-09
    • 2012-06-22
    • 2019-06-30
    • 1970-01-01
    相关资源
    最近更新 更多