7

I would like to modify DB connection creation with Spring Retry to try again if DB is down at application startup. I would not like to limit the number of retries. How should I configure the policy to do that.

My current code (I know in this state it limits to 100):

SimpleRetryPolicy policy = new SimpleRetryPolicy(100, Collections.singletonMap(Exception.class, true));

// Use the policy...
RetryTemplate template = new RetryTemplate();
template.setRetryPolicy(policy);
Connection conn = template.execute(new RetryCallback<Connection, Exception>() {
    public Connection doWithRetry(RetryContext context) throws Exception {
        return getConnectionFactory().createConnection();
    }
});

How should I modify this code?

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
user2695543
  • 81
  • 4
  • 22

1 Answers1

5

Use AlwaysRetryPolicy instead of SimpleRetryPolicy.

But you might want to add a BackOffPolicy to wait between retries.

You can then interrupt the thread to shut everything down.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • How can the `AlwaysRetryPolicy` be specified using the `@Retryable` annotation? – Inego Mar 16 '20 at 09:46
  • 2
    Don't ask new questions in comments; ask a new question instead. You can't; you would have to wire up an interceptor and use the `interceptor` property. Setting `maxAttempts` to `Integer.MAX_VALUE` would achieve something similar. – Gary Russell Mar 16 '20 at 14:16