通过自定义异常解析器处理CannotCreateTransactionException

时间:2019-12-30 13:30:10

标签: java spring hibernate tomcat

我正在使用Spring MVC 4,Hibernate 4和c3p0来限制池数据库连接。我还实现了自定义异常解析器,该解析器将AnnotationMethodHandlerExceptionResolver扩展到Tomcat,不会将日志返回到front(user)。但是,我注意到我的异常解析器无法处理由org.springframework.transaction.CannotCreateTransactionExceptionorg.hibernate.exception.GenericJDBCException引起的异常,因为数据库连接池已耗尽,但是Tomcat将所有日志返回到最前面-用户可见,但是这是不想要的结果。我试图通过在doResolveException中的覆盖方法AnnotationMethodHandlerExceptionResolver中设置的断点来调试异常解析器,但应用程序不会在断点处停止。 Hibernate异常应该以不同的方式处理吗?如何在自定义异常解析器中处理它们?

public class MappingExceptionResolver extends AnnotationMethodHandlerExceptionResolver{

    @Override
    protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handlerMethod, Exception exception) {
        ModelAndView mav = super.doResolveException(request, response, handlerMethod, exception);
        if (isEmpty(mav)) {
            MappingResolveExceptionResult resolveResult = getResolveResult(request, exception);
            response.setStatus(resolveResult.getResponseStatus());
            mav = new ModelAndView(resolveResult.getView());
            if (HttpStatus.valueOf(resolveResult.getResponseStatus()).series() != Series.REDIRECTION) {
                mav.addObject("exception", exception);
                mav.addObject("handler", handlerMethod);
            }
        }
        return mav;
    }
}

0 个答案:

没有答案