From 1dab5e0bbd9cbffa71b6cd800551ba89e3a7f29e Mon Sep 17 00:00:00 2001 From: Karen Chen Date: Tue, 11 Mar 2025 10:20:24 -0700 Subject: [PATCH] fix: verify connection is not a stale writer in the monitor --- .../monitoring/ClusterTopologyMonitorImpl.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/wrapper/src/main/java/software/amazon/jdbc/hostlistprovider/monitoring/ClusterTopologyMonitorImpl.java b/wrapper/src/main/java/software/amazon/jdbc/hostlistprovider/monitoring/ClusterTopologyMonitorImpl.java index 80a586249..055e6c38d 100644 --- a/wrapper/src/main/java/software/amazon/jdbc/hostlistprovider/monitoring/ClusterTopologyMonitorImpl.java +++ b/wrapper/src/main/java/software/amazon/jdbc/hostlistprovider/monitoring/ClusterTopologyMonitorImpl.java @@ -818,6 +818,18 @@ public void run() { connection = null; } + if (!StringUtils.isNullOrEmpty(writerId)) { + try { + if (this.monitor.pluginService.getHostRole(connection) != HostRole.WRITER) { + // The first connection after failover may be stale. + writerId = null; + } + } catch (SQLException e) { + // Invalid connection, retry. + continue; + } + } + if (!StringUtils.isNullOrEmpty(writerId)) { // this prevents closing connection in finally block if (!this.monitor.nodeThreadsWriterConnection.compareAndSet(null, connection)) {