代码之家  ›  专栏  ›  技术社区  ›  hammerva

尝试在hibernate提交处理中使用SQLException时出现问题

  •  0
  • hammerva  · 技术社区  · 5 年前

        if (editRollBackNeeded) {
            try {
                hibernateTransaction.setRollbackOnly();
                hibernateTransaction.rollback();
                returnCode = 16;
                logger.info(" ");
                logger.info("RECORD EDIT ERROR " + "ROLLBACK OCCURRED - INVESTIGATE");
                logger.info(" ");
                message = message + " " + System.lineSeparator() 
                                  + "RECORD EDIT ERROR " + "ROLLBACK OCCURRED - INVESTIGATE " + System.lineSeparator();
                terminateProcess(returnCode, message)  ;   
            } catch (Exception rollbackE) {
                logger.info("ROLLBACK EXCEPTION " + rollbackE.getMessage() + " OCCURRED - INVESTIGATE");
                message = message + " " + System.lineSeparator() 
                                  + "ROLLBACK EXCEPTION WAS " + rollbackE.getMessage() + System.lineSeparator() 
                                  + " " + System.lineSeparator();
            }
        } else {
            try {
                hibernateTransaction.commit();
            } catch (SQLException commitExcep) { 
                commitExcep.printStackTrace(pw);  
                logger.info("COMMIT EXCEPTION WAS " + sw.toString());
                logger.debug("COMMIT LOCAL MESSAGE " + Arrays.asList(commitExcep.getLocalizedMessage()));
                logger.info(sw.toString()) ;
                message = message + " " + System.lineSeparator() 
                                  + "COMMIT EXCEPTION WAS " + sw.toString() + System.lineSeparator() 
                                   + " " + System.lineSeparator();
                if (hibernateTransaction != null) {
                    try {
                        hibernateTransaction.rollback();
                        returnCode = 16;
                        logger.info("SQL ERROR " + "ROLLBACK OCCURRED - INVESTIGATE");
                        logger.info(" ");
                        message = message + " " + System.lineSeparator() 
                                          + "SQL ERROR " + "ROLLBACK OCCURRED - INVESTIGATE " + System.lineSeparator();
                    } catch (Exception rollbackE) {
                        logger.info("ROLLBACK EXCEPTION " + rollbackE.getMessage() + " OCCURRED - INVESTIGATE");
                        returnCode = 16;
                        terminateProcess(returnCode, message)  ;
    
                    }
    
                } else {
                }
            }
        }
    

    它位于Spring批的afterStep部分。我抛出所有异常,特别是SQL异常。我得到一个编译错误,说“SQLExecution永远不会抛出到相应try语句的主体中”。我真的不明白,因为我有这个错误。你能看看我搞砸了什么吗。

    0 回复  |  直到 5 年前
        1
  •  1
  •   mwarren    5 年前

    Hibernates事务类方法commit()引发HibernateeException,而不是SQLException。所以把你的捕获改成冬眠异常。