From: bauna (@bauna) <noreply+bauna@github.com>
To: aws/aws-advanced-jdbc-wrapper <noreply+aws-aws-advanced-jdbc-wrapper@github.com>
Subject: [aws/aws-advanced-jdbc-wrapper] PR #2259: fix: honor skipFailoverOnInterruptedThread when the connection is already closed
Date: Thu, 19 Nov 2026 13:27:30 +0000
Message-ID: <gh-aws-aws-advanced-jdbc-wrapper-2259@github.com> (raw)
### Summary
Fixes #2258. The failover plugins now honor `skipFailoverOnInterruptedThread` on every path to `failover()`, not only in the exception handler.
### Description
When an interrupt closes the socket of a connection, the next JDBC call on that connection goes into `execute()`. The `isClosed()` branch in `execute()` calls `pickNewConnection()`. Before this change, `pickNewConnection()` did not check `skipFailoverOnInterruptedThread`. Thus an interrupted thread started a writer failover. For Aurora, the writer failover also put the topology monitor into panic mode.
Changes:
- `failover2.FailoverConnectionPlugin.pickNewConnection()` and `failover.FailoverConnectionPlugin.pickNewConnection()`: when `skipFailoverOnInterruptedThread=true` and the current thread is interrupted, the method throws `SQLException` with SQLSTATE `08003` and does not call `failover()`. The pool then discards the connection, as the property documentation says. `GlobalDbFailoverConnectionPlugin` extends the `failover2` plugin and gets the same behavior.
Tests:
- `failover.FailoverConnectionPluginTest`: closed connection and interrupted thread, with the property on and off.
- `failover2.FailoverConnectionPluginTest` (new): `execute()` on a closed connection. The tests cover an interrupted thread with the property on and off, and a thread that is not interrupted.
Without the fix, the 2 new tests for an interrupted thread with the property on fail. With the fix, all unit tests and checkstyle pass.
The `Repro.java` from #2258 against `postgres:16-alpine` with `wrapperDialect=pg` gives this output.
Before (`main`):
```
FINE: Do not start failover since the current thread is interrupted.
query failed: 08006 An I/O error occurred while sending to the backend., interrupted=true, isClosed=true
INFO: Starting writer failover procedure.
INFO: Starting writer failover procedure.
rollback failed: software.amazon.jdbc.plugin.failover.FailoverFailedSQLException: Not able to establish a connection before timing out.
```
After (this branch):
```
FINE: Do not start failover since the current thread is interrupted.
query failed: 08006 An I/O error occurred while sending to the backend., interrupted=true, isClosed=true
FINE: Do not start failover since the current thread is interrupted.
rollback failed: java.sql.SQLException: Do not start failover since the current thread is interrupted.
```
### Additional Reviewers
### By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
inline patch 1: wrapper/src/main/java/software/amazon/jdbc/plugin/failover/FailoverConnectionPlugin.java, wrapper/src/main/java/software/amazon/jdbc/plugin/failover2/FailoverConnectionPlugin.java, wrapper/src/test/java/software/amazon/jdbc/plugin/failover/FailoverConnectionPluginTest.java and 1 more (+199/-0 lines) download inline-1.patchdiff --git a/wrapper/src/main/java/software/amazon/jdbc/plugin/failover/FailoverConnectionPlugin.java b/wrapper/src/main/java/software/amazon/jdbc/plugin/failover/FailoverConnectionPlugin.javaindex 79666454f..f64bd2f7f 100644--- a/wrapper/src/main/java/software/amazon/jdbc/plugin/failover/FailoverConnectionPlugin.java+++ b/wrapper/src/main/java/software/amazon/jdbc/plugin/failover/FailoverConnectionPlugin.java@@ -887,6 +887,13 @@ protected void pickNewConnection() throws SQLException {
return;
}
+ if (this.skipFailoverOnInterruptedThread && Thread.currentThread().isInterrupted()) {+ LOGGER.fine(() -> Messages.get("Failover.skipFailoverOnInterruptedThread"));+ throw new SQLException(+ Messages.get("Failover.skipFailoverOnInterruptedThread"),+ SqlState.CONNECTION_NOT_OPEN.getState());+ }+
if (this.pluginService.getCurrentConnection() == null && !shouldAttemptReaderConnection()) {
final HostSpec writerHost = Utils.getWriter(this.pluginService.getAllHosts());
if (writerHost != null) {
diff --git a/wrapper/src/main/java/software/amazon/jdbc/plugin/failover2/FailoverConnectionPlugin.java b/wrapper/src/main/java/software/amazon/jdbc/plugin/failover2/FailoverConnectionPlugin.javaindex 0956905e3..03ea9f094 100644--- a/wrapper/src/main/java/software/amazon/jdbc/plugin/failover2/FailoverConnectionPlugin.java+++ b/wrapper/src/main/java/software/amazon/jdbc/plugin/failover2/FailoverConnectionPlugin.java@@ -795,6 +795,13 @@ protected void pickNewConnection() throws SQLException {
return;
}
+ if (this.skipFailoverOnInterruptedThread && Thread.currentThread().isInterrupted()) {+ LOGGER.fine(() -> Messages.get("Failover.skipFailoverOnInterruptedThread"));+ throw new SQLException(+ Messages.get("Failover.skipFailoverOnInterruptedThread"),+ SqlState.CONNECTION_NOT_OPEN.getState());+ }+
this.failover();
}
diff --git a/wrapper/src/test/java/software/amazon/jdbc/plugin/failover/FailoverConnectionPluginTest.java b/wrapper/src/test/java/software/amazon/jdbc/plugin/failover/FailoverConnectionPluginTest.javaindex 83240b6b8..7cc655b37 100644--- a/wrapper/src/test/java/software/amazon/jdbc/plugin/failover/FailoverConnectionPluginTest.java+++ b/wrapper/src/test/java/software/amazon/jdbc/plugin/failover/FailoverConnectionPluginTest.java@@ -459,6 +459,53 @@ void test_execute_withDirectExecute() throws SQLException {
verify(mockHostListProvider, never()).getRdsUrlType();
}
+ @Test+ void test_execute_closedConnection_interruptedThread_skipFailover() throws SQLException {+ properties.setProperty(FailoverConnectionPlugin.SKIP_FAILOVER_ON_INTERRUPTED_THREAD.name, "true");+ when(mockConnection.isClosed()).thenReturn(true);+ initializePlugin();++ Thread.currentThread().interrupt();+ try {+ final SQLException exception = assertThrows(SQLException.class, () -> spyPlugin.execute(+ ResultSet.class,+ SQLException.class,+ MONITOR_METHOD_INVOKE_ON,+ "Connection.rollback",+ mockSqlFunction,+ EMPTY_ARGS));+ assertEquals(SqlState.CONNECTION_NOT_OPEN.getState(), exception.getSQLState());+ assertTrue(Thread.currentThread().isInterrupted());+ } finally {+ Thread.interrupted();+ }++ verify(spyPlugin, never()).failover(any(), anyBoolean());+ verify(mockSqlFunction, never()).call();+ }++ @Test+ void test_execute_closedConnection_interruptedThread_skipFailoverDisabled() throws SQLException {+ when(mockConnection.isClosed()).thenReturn(true);+ initializePlugin();+ doThrow(new FailoverSuccessSQLException()).when(spyPlugin).failover(any(), anyBoolean());++ Thread.currentThread().interrupt();+ try {+ assertThrows(FailoverSuccessSQLException.class, () -> spyPlugin.execute(+ ResultSet.class,+ SQLException.class,+ MONITOR_METHOD_INVOKE_ON,+ "Connection.rollback",+ mockSqlFunction,+ EMPTY_ARGS));+ } finally {+ Thread.interrupted();+ }++ verify(spyPlugin).failover(any(), anyBoolean());+ }+
private void initializePlugin() {
spyPlugin = spy(new FailoverConnectionPlugin(mockContainer, properties));
spyPlugin.setWriterFailoverHandler(mockWriterFailoverHandler);
diff --git a/wrapper/src/test/java/software/amazon/jdbc/plugin/failover2/FailoverConnectionPluginTest.java b/wrapper/src/test/java/software/amazon/jdbc/plugin/failover2/FailoverConnectionPluginTest.javanew file mode 100644index 000000000..7df1219e4--- /dev/null+++ b/wrapper/src/test/java/software/amazon/jdbc/plugin/failover2/FailoverConnectionPluginTest.java@@ -0,0 +1,138 @@+/*+ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.+ *+ * Licensed under the Apache License, Version 2.0 (the "License").+ * You may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++package software.amazon.jdbc.plugin.failover2;++import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.junit.jupiter.api.Assertions.assertThrows;+import static org.junit.jupiter.api.Assertions.assertTrue;+import static org.mockito.ArgumentMatchers.any;+import static org.mockito.ArgumentMatchers.anyString;+import static org.mockito.Mockito.doThrow;+import static org.mockito.Mockito.never;+import static org.mockito.Mockito.spy;+import static org.mockito.Mockito.verify;+import static org.mockito.Mockito.when;++import java.sql.Connection;+import java.sql.SQLException;+import java.util.HashSet;+import java.util.Properties;+import org.junit.jupiter.api.AfterEach;+import org.junit.jupiter.api.BeforeEach;+import org.junit.jupiter.api.Test;+import org.mockito.Mock;+import org.mockito.MockitoAnnotations;+import software.amazon.jdbc.JdbcCallable;+import software.amazon.jdbc.PluginService;+import software.amazon.jdbc.plugin.failover.FailoverSuccessSQLException;+import software.amazon.jdbc.targetdriverdialect.TargetDriverDialect;+import software.amazon.jdbc.util.FullServicesContainer;+import software.amazon.jdbc.util.SqlState;+import software.amazon.jdbc.util.telemetry.TelemetryCounter;+import software.amazon.jdbc.util.telemetry.TelemetryFactory;++class FailoverConnectionPluginTest {++ private static final Object[] EMPTY_ARGS = {};+ private static final String ROLLBACK_METHOD_NAME = "Connection.rollback";++ @Mock FullServicesContainer mockContainer;+ @Mock PluginService mockPluginService;+ @Mock Connection mockConnection;+ @Mock JdbcCallable<Void, SQLException> mockSqlFunction;+ @Mock TelemetryFactory mockTelemetryFactory;+ @Mock TelemetryCounter mockTelemetryCounter;+ @Mock TargetDriverDialect mockTargetDriverDialect;++ private final Properties properties = new Properties();+ private AutoCloseable closeable;++ @BeforeEach+ void init() throws SQLException {+ closeable = MockitoAnnotations.openMocks(this);++ when(mockContainer.getPluginService()).thenReturn(mockPluginService);+ when(mockPluginService.getCurrentConnection()).thenReturn(mockConnection);+ when(mockPluginService.getTelemetryFactory()).thenReturn(mockTelemetryFactory);+ when(mockTelemetryFactory.createCounter(anyString())).thenReturn(mockTelemetryCounter);+ when(mockPluginService.getTargetDriverDialect()).thenReturn(mockTargetDriverDialect);+ when(mockTargetDriverDialect.getNetworkBoundMethodNames(any())).thenReturn(new HashSet<>());+ when(mockConnection.isClosed()).thenReturn(true);++ properties.clear();+ }++ @AfterEach+ void cleanUp() throws Exception {+ Thread.interrupted();+ closeable.close();+ }++ @Test+ void test_execute_closedConnection_interruptedThread_skipFailover() throws SQLException {+ properties.setProperty(FailoverConnectionPlugin.SKIP_FAILOVER_ON_INTERRUPTED_THREAD.name, "true");+ final FailoverConnectionPlugin spyPlugin = spy(new FailoverConnectionPlugin(mockContainer, properties));++ Thread.currentThread().interrupt();+ final SQLException exception = assertThrows(SQLException.class, () -> spyPlugin.execute(+ Void.class,+ SQLException.class,+ mockConnection,+ ROLLBACK_METHOD_NAME,+ mockSqlFunction,+ EMPTY_ARGS));++ assertEquals(SqlState.CONNECTION_NOT_OPEN.getState(), exception.getSQLState());+ assertTrue(Thread.currentThread().isInterrupted());+ verify(spyPlugin, never()).failover();+ verify(mockSqlFunction, never()).call();+ }++ @Test+ void test_execute_closedConnection_interruptedThread_skipFailoverDisabled() throws SQLException {+ final FailoverConnectionPlugin spyPlugin = spy(new FailoverConnectionPlugin(mockContainer, properties));+ doThrow(new FailoverSuccessSQLException()).when(spyPlugin).failover();++ Thread.currentThread().interrupt();+ assertThrows(FailoverSuccessSQLException.class, () -> spyPlugin.execute(+ Void.class,+ SQLException.class,+ mockConnection,+ ROLLBACK_METHOD_NAME,+ mockSqlFunction,+ EMPTY_ARGS));++ verify(spyPlugin).failover();+ }++ @Test+ void test_execute_closedConnection_notInterrupted_startsFailover() throws SQLException {+ properties.setProperty(FailoverConnectionPlugin.SKIP_FAILOVER_ON_INTERRUPTED_THREAD.name, "true");+ final FailoverConnectionPlugin spyPlugin = spy(new FailoverConnectionPlugin(mockContainer, properties));+ doThrow(new FailoverSuccessSQLException()).when(spyPlugin).failover();++ assertThrows(FailoverSuccessSQLException.class, () -> spyPlugin.execute(+ Void.class,+ SQLException.class,+ mockConnection,+ ROLLBACK_METHOD_NAME,+ mockSqlFunction,+ EMPTY_ARGS));++ verify(spyPlugin).failover();+ }+}