2015-07-02 33 views
2

我在Amazon EC2上託管Spring引導應用程序。有時候,當我去的網頁,早上我會看到Java Spring在發生500錯誤時堅持棧跟蹤

「無法打開JPA EntityManager的事務;嵌套的例外是javax.persistence.PersistenceException:org.hibernate.TransactionException:JDBC開始事務失敗:」

來自瀏覽器的

。但是我無法找回堆棧跟蹤。所以在Spring中這是可能的:當發生500錯誤時,Spring會捕獲異常並將其存儲在數據庫或本地文件中,以便稍後再回來。我認爲這將有助於調試難以複製的500錯誤。

回答

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; 
    } 
    }