From: sergiyvamz (@sergiyvamz) <noreply+sergiyvamz@github.com>
To: aws/aws-advanced-jdbc-wrapper <noreply+aws-aws-advanced-jdbc-wrapper@github.com>
Subject: [aws/aws-advanced-jdbc-wrapper] PR #2264: fix: skip failover on interrupted thread when connection is closed
Date: Thu, 19 Nov 2026 23:56:29 +0000
Message-ID: <gh-aws-aws-advanced-jdbc-wrapper-2264@github.com> (raw)
Fixes #2258.
`skipFailoverOnInterruptedThread` was only checked on the exception path. An interrupt can close the socket, so the next call on the same connection (for example, the pool's `rollback()`) reached `pickNewConnection()` through the closed-connection check in `execute()` and started a failover on a healthy cluster.
`pickNewConnection()` in `failover` and `failover2` (and so `gdbFailover`) now throws a `SQLException` with SQLSTATE 08003 instead when the thread is interrupted and the setting is enabled, so the pool discards the connection. Behavior is unchanged when the setting is off or the thread is not interrupted.
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/main/resources/aws_advanced_jdbc_wrapper_messages.properties and 2 more (+206/-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..b6542e2b1 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,16 @@ protected void pickNewConnection() throws SQLException {
return;
}
+ // Honor skipFailoverOnInterruptedThread on every path that leads to failover, not only on the+ // exception path. An interrupt can close the underlying socket, so the next call on the connection+ // (for example, a pool's rollback) would otherwise start a failover on a healthy cluster.+ if (this.skipFailoverOnInterruptedThread && Thread.currentThread().isInterrupted()) {+ LOGGER.fine(() -> Messages.get("Failover.skipFailoverOnInterruptedThread"));+ throw new SQLException(+ Messages.get("Failover.connectionNotOpenOnInterruptedThread"),+ 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..37e31716c 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,16 @@ protected void pickNewConnection() throws SQLException {
return;
}
+ // Honor skipFailoverOnInterruptedThread on every path that leads to failover, not only on the+ // exception path. An interrupt can close the underlying socket, so the next call on the connection+ // (for example, a pool's rollback) would otherwise start a failover on a healthy cluster.+ if (this.skipFailoverOnInterruptedThread && Thread.currentThread().isInterrupted()) {+ LOGGER.fine(() -> Messages.get("Failover.skipFailoverOnInterruptedThread"));+ throw new SQLException(+ Messages.get("Failover.connectionNotOpenOnInterruptedThread"),+ SqlState.CONNECTION_NOT_OPEN.getState());+ }+
this.failover();
}
diff --git a/wrapper/src/main/resources/aws_advanced_jdbc_wrapper_messages.properties b/wrapper/src/main/resources/aws_advanced_jdbc_wrapper_messages.propertiesindex b1e4ac35c..952146c1d 100644--- a/wrapper/src/main/resources/aws_advanced_jdbc_wrapper_messages.properties+++ b/wrapper/src/main/resources/aws_advanced_jdbc_wrapper_messages.properties@@ -333,6 +333,7 @@ Failover.writerFailoverElapsed=Writer failover elapsed in {0} ms.
Failover.failedReaderConnection=[Reader Failover] Failed to connect to host: ''{0}''
Failover.errorSelectingReaderHost=An error occurred while attempting to select a reader host candidate: ''{0}''. Candidates:
Failover.skipFailoverOnInterruptedThread=Do not start failover since the current thread is interrupted.
+Failover.connectionNotOpenOnInterruptedThread=The connection is closed and failover was skipped because the current thread is interrupted and ''skipFailoverOnInterruptedThread'' is enabled. Dispose of this connection.
GlobalAuroraTopologyMonitor.cannotFindRegionTemplate=Cannot find cluster template for region {0}.
GlobalAuroraTopologyMonitor.initialHostNotInAccessibleRegion=Initial host ''{0}'' is in region ''{1}'' which is not in the list of accessible regions {2}.
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..58a6df16b 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("skipFailoverOnInterruptedThread", "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..417fc3fe4--- /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.ResultSet;+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 = "Connection.rollback";++ @Mock FullServicesContainer mockContainer;+ @Mock PluginService mockPluginService;+ @Mock Connection mockConnection;+ @Mock TelemetryFactory mockTelemetryFactory;+ @Mock TelemetryCounter mockTelemetryCounter;+ @Mock TargetDriverDialect mockTargetDriverDialect;+ @Mock JdbcCallable<ResultSet, SQLException> mockSqlFunction;++ private final Properties properties = new Properties();+ private AutoCloseable closeable;++ @BeforeEach+ void init() {+ closeable = MockitoAnnotations.openMocks(this);+ when(mockContainer.getPluginService()).thenReturn(mockPluginService);+ when(mockPluginService.getTelemetryFactory()).thenReturn(mockTelemetryFactory);+ when(mockTelemetryFactory.createCounter(anyString())).thenReturn(mockTelemetryCounter);+ when(mockPluginService.getTargetDriverDialect()).thenReturn(mockTargetDriverDialect);+ when(mockTargetDriverDialect.getNetworkBoundMethodNames(any())).thenReturn(new HashSet<>());+ when(mockPluginService.getCurrentConnection()).thenReturn(mockConnection);+ properties.clear();+ }++ @AfterEach+ void cleanUp() throws Exception {+ // Never leak the interrupt flag into other tests.+ Thread.interrupted();+ closeable.close();+ }++ @Test+ void test_execute_closedConnection_interruptedThread_skipFailover() throws SQLException {+ properties.setProperty("skipFailoverOnInterruptedThread", "true");+ when(mockConnection.isClosed()).thenReturn(true);+ final FailoverConnectionPlugin spyPlugin = spy(new FailoverConnectionPlugin(mockContainer, properties));++ Thread.currentThread().interrupt();+ final SQLException exception = assertThrows(SQLException.class, () -> spyPlugin.execute(+ ResultSet.class, SQLException.class, mockConnection, ROLLBACK, 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_invalidInvocationOnClosedConnection_interruptedThread_skipFailover() throws SQLException {+ properties.setProperty("skipFailoverOnInterruptedThread", "true");+ final FailoverConnectionPlugin spyPlugin = spy(new FailoverConnectionPlugin(mockContainer, properties));++ Thread.currentThread().interrupt();+ final SQLException exception =+ assertThrows(SQLException.class, spyPlugin::invalidInvocationOnClosedConnection);++ assertEquals(SqlState.CONNECTION_NOT_OPEN.getState(), exception.getSQLState());+ verify(spyPlugin, never()).failover();+ }++ @Test+ void test_execute_closedConnection_interruptedThread_skipFailoverDisabled() throws SQLException {+ when(mockConnection.isClosed()).thenReturn(true);+ final FailoverConnectionPlugin spyPlugin = spy(new FailoverConnectionPlugin(mockContainer, properties));+ doThrow(new FailoverSuccessSQLException()).when(spyPlugin).failover();++ Thread.currentThread().interrupt();+ assertThrows(FailoverSuccessSQLException.class, () -> spyPlugin.execute(+ ResultSet.class, SQLException.class, mockConnection, ROLLBACK, mockSqlFunction, EMPTY_ARGS));++ verify(spyPlugin).failover();+ }++ @Test+ void test_execute_closedConnection_notInterrupted_skipFailoverEnabled() throws SQLException {+ properties.setProperty("skipFailoverOnInterruptedThread", "true");+ when(mockConnection.isClosed()).thenReturn(true);+ final FailoverConnectionPlugin spyPlugin = spy(new FailoverConnectionPlugin(mockContainer, properties));+ doThrow(new FailoverSuccessSQLException()).when(spyPlugin).failover();++ assertThrows(FailoverSuccessSQLException.class, () -> spyPlugin.execute(+ ResultSet.class, SQLException.class, mockConnection, ROLLBACK, mockSqlFunction, EMPTY_ARGS));++ verify(spyPlugin).failover();+ }+}