Spring retry connection until datasource is available Spring retry connection until datasource is available spring spring

Spring retry connection until datasource is available


Set HikariCP's initializationFailTimeout property to 0 (zero), or a negative number. As documented here:

initializationFailTimeout

This property controls whether the pool will "fail fast" if the pool cannot be seeded with an initial connection successfully. Any positive number is taken to be the number of milliseconds to attempt to acquire an initial connection; the application thread will be blocked during this period. If a connection cannot be acquired before this timeout occurs, an exception will be thrown. This timeout is applied after the connectionTimeout period. If the value is zero (0), HikariCP will attempt to obtain and validate a connection. If a connection is obtained, but fails validation, an exception will be thrown and the pool not started. However, if a connection cannot be obtained, the pool will start, but later efforts to obtain a connection may fail. A value less than zero will bypass any initial connection attempt, and the pool will start immediately while trying to obtain connections in the background. Consequently, later efforts to obtain a connection may fail. Default: 1


There is an alternative way to do this, which doesn't rely on a specific Connection Pool library or a specific database. Note that you will need to use spring-retry to achieve the desired behaviour with this approach

First you need to add spring-retry to your dependencies :

<dependency>    <groupId>org.springframework.retry</groupId>    <artifactId>spring-retry</artifactId>    <version>${spring-retry.version}</version></dependency>

Then you can create a decorator over DataSource that will extends AbstractDataSource like bellow :

@Slf4j@RequiredArgsConstructorpublic class RetryableDataSource extends AbstractDataSource {    private final DataSource dataSource;    @Override    @Retryable(maxAttempts = 5, backoff = @Backoff(multiplier = 1.3, maxDelay = 10000))    public Connection getConnection() throws SQLException {        log.info("getting connection ...");        return dataSource.getConnection();    }    @Override    @Retryable(maxAttempts = 5, backoff = @Backoff(multiplier = 2.3, maxDelay = 10000))    public Connection getConnection(String username, String password) throws SQLException {        log.info("getting connection by username and password ...");        return dataSource.getConnection(username, password);    }}

Then you will need to inject this custom DataSource decorator into Spring context by creating a custom BeanPostProcessor :

@Slf4j@Order(value = Ordered.HIGHEST_PRECEDENCE)@Componentpublic class RetryableDatabasePostProcessor implements BeanPostProcessor {    @Override    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {        if(bean instanceof DataSource) {            log.info("-----> configuring a retryable datasource for beanName = {}", beanName);            return new RetryableDataSource((DataSource) bean);        }        return bean;    }    @Override    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {        return bean;    }}

Last but not least you will need to enable Spring retry by adding @EnableRetry annotation to spring main class, example :

@EnableRetry@SpringBootApplicationpublic class RetryableDbConnectionApplication {    public static void main(String[] args) {        SpringApplication.run(RetryableDbConnectionApplication.class, args);    }}