Spring retry connection until datasource is available : <p>I have a docker-compose setup to start my SpringBoot application and a MySQL database. If the database starts first, then my application can connect successfully. But if my application starts first, no database exists yet, so the application throws the following exception and exits:</p>

<pre><code>app_1       | 2018-05-27 14:15:03.415  INFO 1 --- [           main]
com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
app_1       | 2018-05-27 14:15:06.770 ERROR 1 --- [           main]
com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Exception during pool initialization
app_1       | com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
Communications link failure
</code></pre>

<p>I could edit my docker-compose file to make sure the database is always up before the application starts up, but I want the application to be able to handle this case on its own, and not immediately exit when it cannot reach the database address.</p>

<p>There are ways to configure the datasource in the application.properties file to make the application reconnect to the database, as answered <a href="https://stackoverflow.com/a/33213802/6566891">here</a> and <a href="https://stackoverflow.com/a/22687418/6566891">here</a>. But that doesn't work for a startup connection to the datasource.</p>

<p>How can I make my SpringBoot application retry the connection at startup to the database at a given interval until it successfully connects to the database?</p>
