javapostgresqlspring-bootexceptionconnectionexception

How to handle database connection exception globally in springboot application?


I have a springboot + postgres application. I want to handle database failure exception globally and need to store request in file system. Is there any way in springboot application to catch ConnectionException globally?


Solution

  • import javax.ws.rs.core.Response;
    
       import javax.ws.rs.ext.ExceptionMapper;
       import javax.ws.rs.ext.Provider;
    
       @Provider
       public class ExceptionHandler  implements ExceptionMapper<ConnectionException > {
      
      @Override
      public Response toResponse(Exception exception) {
        //insert code here
        return Response.ok().build();
      }
    }