DBZ-8058 Connection will be validated and re-connect if not valid

This commit is contained in:
mfvitale 2024-07-12 14:47:23 +02:00 committed by Chris Cranford
parent 7552f815b0
commit fb8150e617
2 changed files with 24 additions and 4 deletions

View File

@ -404,6 +404,15 @@ public JdbcConnection connect() throws SQLException {
return this;
}
/**
* Ensure a connection to the database is established again.
*
* @throws SQLException if there is an error connecting to the database
*/
public void reconnect() throws SQLException {
establishConnection();
}
/**
* Execute a series of SQL statements as a single transaction.
*
@ -891,10 +900,9 @@ public synchronized Connection connection() throws SQLException {
public synchronized Connection connection(boolean executeOnConnect) throws SQLException {
if (!isConnected()) {
conn = factory.connect(JdbcConfiguration.adapt(config));
if (!isConnected()) {
throw new SQLException("Unable to obtain a JDBC connection");
}
establishConnection();
// Always run the initial operations on this new connection
if (initialOps != null) {
execute(initialOps);
@ -908,6 +916,13 @@ public synchronized Connection connection(boolean executeOnConnect) throws SQLEx
return conn;
}
private void establishConnection() throws SQLException {
conn = factory.connect(JdbcConfiguration.adapt(config));
if (!isConnected()) {
throw new SQLException("Unable to obtain a JDBC connection");
}
}
protected List<String> parseSqlStatementString(final String statements) {
final List<String> splitStatements = new ArrayList<>();
final char[] statementsChars = statements.toCharArray();

View File

@ -238,6 +238,11 @@ private Queue<JdbcConnection> createConnectionPool(final RelationalSnapshotConte
}
public Connection createSnapshotConnection() throws SQLException {
if (!jdbcConnection.isValid()) {
jdbcConnection.reconnect();
}
Connection connection = jdbcConnection.connection();
connection.setAutoCommit(false);
return connection;