【问题标题】:Java Spring persist stack trace when 500 error happens发生 500 错误时,Java Spring 坚持堆栈跟踪
【发布时间】:2015-07-02 00:21:05
【问题描述】:

我在 Amazon EC2 上托管一个 Spring Boot 应用程序。有时早上我去网页时会看到

“无法为事务打开 JPA EntityManager;嵌套异常是 javax.persistence.PersistenceException: org.hibernate.TransactionException: JDBC begin transaction failed:”

来自浏览器。但是我无法取回堆栈跟踪。那么这在 Spring 中是否可能:当 500 错误发生时,spring 会捕获异常并将其存储在数据库或本地文件中,以便稍后我可以将其取回。我认为这将有助于调试难以重现的 500 错误。

【问题讨论】:

    标签: spring spring-boot


    【解决方案1】:

    是的,您可能只需要配置控制器以使用 Spring 异常处理 https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc

    然后你可以配置你想要捕获的异常级别(在你的情况下一般的异常是可以的,或者如果你更了解特定的异常)

        // Total control - setup a model and return the view name yourself. Or consider
        // subclassing ExceptionHandlerExceptionResolver (see below).
        @ExceptionHandler(Exception.class)
         public ModelAndView handleError(HttpServletRequest req, Exception exception) {
           logger.error("Request: " + req.getRequestURL() + " raised " + exception);
           //Here you can persist the exception or just write in the log
           ModelAndView mav = new ModelAndView();
           mav.addObject("exception", exception);
           mav.addObject("url", req.getRequestURL());
           mav.setViewName("error");
           return mav;
         }
       }
    

    【讨论】:

      猜你喜欢
      • 2017-10-14
      • 2018-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-30
      • 1970-01-01
      • 2019-05-02
      • 2015-11-19
      相关资源
      最近更新 更多