简体   繁体   中英

Catching JDBCConnectionExceptions in Spring boot

In spring boot with hikari handling database connections, i want to catch, JDBCConnectionException exceptions or any other hibernate level exceptions, so that i can generate notifications when this happens. I have one @ControllerAdvice configured but, seems it doesn't come to that level but handled earlier.

@Order(Ordered.HIGHEST_PRECEDENCE)
@ControllerAdvice
public class ExceptionTranslator {

    @ExceptionHandler(JDBCConnectionException.class)
    public void handleJdbcConnectionException(JDBCConnectionException ex, HttpServletResponse response) throws IOException {
        response.sendError(HttpStatus.INTERNAL_SERVER_ERROR.value(), ex.getMessage());
    }

I am using spring data repositories to access db.

WARN |PoolBase                      |2d33dd4e-46d6-11e9-9d35-2f44d962868c|conf-hikari-pool - Failed to validate connection ConnectionID:1 ClientConnectionId: 1f1135cd-94bf-4543-bfe8-687e10a40797 (The connection is closed.). Possibly consider using a shorter maxLifetime value.
WARN |PoolBase                      |2d33dd4e-46d6-11e9-9d35-2f44d962868c|conf-hikari-pool - Failed to validate connection ConnectionID:2 ClientConnectionId: 28890cd1-16ad-43f9-b240-a9ae3aa2d45f (The connection is closed.). Possibly consider using a shorter maxLifetime value.
WARN |SqlExceptionHelper            |2d33dd4e-46d6-11e9-9d35-2f44d962868c|SQL Error: 0, SQLState: null
ERROR|SqlExceptionHelper            |2d33dd4e-46d6-11e9-9d35-2f44d962868c|conf-hikari-pool - Connection is not available, request timed out after 6010ms.
ERROR|SqlExceptionHelper            |2d33dd4e-46d6-11e9-9d35-2f44d962868c|The connection is closed.

The code example that you have given will only catch exceptions that are thrown in the flow of a REST call. Many of the spring-data exceptions are thrown after the REST calls return during database commit. It is not possible to throw those exceptions in this fashion. The best way to handle those errors is to look at logs, push them to something like Splunk and generate alerts there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM