-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create new connection on connection validation error in postgres (
#122)
- Loading branch information
1 parent
e80f843
commit a42561f
Showing
4 changed files
with
157 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
document-store/src/main/java/org/hypertrace/core/documentstore/postgres/PostgresClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package org.hypertrace.core.documentstore.postgres; | ||
|
||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.time.Duration; | ||
import java.util.concurrent.TimeUnit; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
class PostgresClient { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(PostgresClient.class); | ||
private static final int VALIDATION_QUERY_TIMEOUT_SECONDS = 5; | ||
|
||
private final String url; | ||
private final String user; | ||
private final String password; | ||
private final int maxConnectionAttempts; | ||
private final Duration connectionRetryBackoff; | ||
|
||
private int count = 0; | ||
private Connection connection; | ||
|
||
public PostgresClient( | ||
String url, | ||
String user, | ||
String password, | ||
int maxConnectionAttempts, | ||
Duration connectionRetryBackoff) { | ||
this.url = url; | ||
this.user = user; | ||
this.password = password; | ||
this.maxConnectionAttempts = maxConnectionAttempts; | ||
this.connectionRetryBackoff = connectionRetryBackoff; | ||
} | ||
|
||
public synchronized Connection getConnection() { | ||
try { | ||
if (connection == null) { | ||
newConnection(); | ||
} else if (!isConnectionValid(connection)) { | ||
log.info("The database connection is invalid. Reconnecting..."); | ||
close(); | ||
newConnection(); | ||
} | ||
} catch (SQLException sqle) { | ||
throw new RuntimeException(sqle); | ||
} | ||
return connection; | ||
} | ||
|
||
private boolean isConnectionValid(Connection connection) { | ||
try { | ||
if (connection.getMetaData().getJDBCMajorVersion() >= 4) { | ||
return connection.isValid(VALIDATION_QUERY_TIMEOUT_SECONDS); | ||
} else { | ||
try (PreparedStatement preparedStatement = connection.prepareStatement("SELECT 1"); | ||
ResultSet resultSet = preparedStatement.executeQuery()) { | ||
return true; | ||
} | ||
} | ||
} catch (SQLException sqle) { | ||
log.debug("Unable to check if the underlying connection is valid", sqle); | ||
return false; | ||
} | ||
} | ||
|
||
private void newConnection() throws SQLException { | ||
++count; | ||
int attempts = 0; | ||
while (attempts < maxConnectionAttempts) { | ||
try { | ||
++attempts; | ||
log.info("Attempting(attempt #{}) to open connection #{} to {}", attempts, count, url); | ||
connection = DriverManager.getConnection(url, user, password); | ||
return; | ||
} catch (SQLException sqle) { | ||
attempts++; | ||
if (attempts < maxConnectionAttempts) { | ||
log.info( | ||
"Unable to connect(#{}) to database on attempt {}/{}. Will retry in {} ms.", | ||
count, | ||
attempts, | ||
maxConnectionAttempts, | ||
connectionRetryBackoff, | ||
sqle); | ||
try { | ||
TimeUnit.MILLISECONDS.sleep(connectionRetryBackoff.toMillis()); | ||
} catch (InterruptedException e) { | ||
// this is ok because just woke up early | ||
} | ||
} else { | ||
throw sqle; | ||
} | ||
} | ||
} | ||
} | ||
|
||
private void close() { | ||
if (connection != null) { | ||
try { | ||
log.info("Closing connection #{} to {}", count, url); | ||
connection.close(); | ||
} catch (SQLException sqle) { | ||
log.warn("Ignoring error closing connection", sqle); | ||
} finally { | ||
connection = null; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters