use of org.flywaydb.core.internal.strategy.BackoffStrategy in project flyway by flyway.
the class JdbcUtils method openConnection.
/**
* Opens a new connection from this DataSource.
*
* @param dataSource The DataSource to obtain the connection from.
* @param connectRetries The maximum number of retries when attempting to connect to the database.
* @param connectRetriesInterval The maximum time between retries in seconds.
* @return The new connection.
* @throws FlywayException when the connection could not be opened.
*/
public static Connection openConnection(DataSource dataSource, int connectRetries, int connectRetriesInterval) throws FlywayException {
BackoffStrategy backoffStrategy = new BackoffStrategy(1, 2, connectRetriesInterval);
int retries = 0;
while (true) {
try {
return dataSource.getConnection();
} catch (SQLException e) {
if ("08S01".equals(e.getSQLState()) && e.getMessage().contains("This driver is not configured for integrated authentication")) {
throw new FlywaySqlException("Unable to obtain connection from database" + getDataSourceInfo(dataSource, true) + ": " + e.getMessage() + "\nTo setup integrated authentication see " + FlywayDbWebsiteLinks.WINDOWS_AUTH, e);
} else if (e.getSQLState() == null && e.getMessage().contains("MSAL4J")) {
throw new FlywaySqlException("Unable to obtain connection from database" + getDataSourceInfo(dataSource, false) + ": " + e.getMessage() + "\nYou need to install some extra drivers in order for interactive authentication to work." + "\nFor instructions, see " + FlywayDbWebsiteLinks.AZURE_ACTIVE_DIRECTORY, e);
}
if (++retries > connectRetries) {
throw new FlywaySqlException("Unable to obtain connection from database" + getDataSourceInfo(dataSource, false) + ": " + e.getMessage(), e);
}
Throwable rootCause = ExceptionUtils.getRootCause(e);
String message = "Connection error: " + e.getMessage();
if (rootCause != null && rootCause != e && rootCause.getMessage() != null) {
message += "\n(Caused by " + rootCause.getMessage() + ")";
}
LOG.warn(message + "\nRetrying in " + backoffStrategy.peek() + " sec...");
try {
Thread.sleep(backoffStrategy.next() * 1000L);
} catch (InterruptedException e1) {
throw new FlywaySqlException("Unable to obtain connection from database" + getDataSourceInfo(dataSource, false) + ": " + e.getMessage(), e);
}
}
}
}
Aggregations