Search code examples
javaspringspring-jdbckafka-consumer-apispring-kafka

Spring JDBC - Retriable Exceptions for Kafka Consumer


I am using a Spring Kafka consumer to read messages off Kafka topic. I am persisting these in Oracle DB. Whenever there is a DB connection error , I want to perform a retry.I am using Spring JDBC to connect to oracle DB.What are the list of exception classes that I need to add in case if I need perform retry only JDBC connection issues.

private static Map<Class<? extends Throwable>, Boolean> retryableExceptions;

static{
    retryableExceptions = new HashMap<>();
    retryableExceptions.put(Exception.class, true);
}
protected RetryPolicy retryPolicy() {
        SimpleRetryPolicy policy = new SimpleRetryPolicy(maxRetryAttempts, retryableExceptions);
        return policy;
}

Solution

  • I think you need just only this one:

    /**
     * Data access exception thrown when a resource fails completely:
     * for example, if we can't connect to a database using JDBC.
     *
     * @author Rod Johnson
     * @author Thomas Risberg
     */
    @SuppressWarnings("serial")
    public class DataAccessResourceFailureException extends NonTransientDataAccessResourceException {
    

    As long as you use JdbcTemplate to perform JDBC operation, any low level error with the connection are wrapped to this DataAccessResourceFailureException or its subclasses.