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)) {