From 9f2f1500e224a1b54648ad9567bb9d8b8a89cec8 Mon Sep 17 00:00:00 2001
From: dcy <dcy10000@gmail.com>
Date: Fri, 13 Sep 2024 15:44:28 +0800
Subject: [PATCH] =?UTF-8?q?inner-2407=EF=BC=9A=20need=20init=20connection?=
 =?UTF-8?q?=20pool=20when=20switch=20with=20config=20rwsplitMode=3D0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../actiontech/dble/backend/datasource/PhysicalDbGroup.java | 4 ++++
 .../dble/backend/datasource/PhysicalDbInstance.java         | 4 ++++
 .../com/actiontech/dble/backend/pool/ConnectionPool.java    | 6 +++++-
 3 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/src/main/java/com/actiontech/dble/backend/datasource/PhysicalDbGroup.java b/src/main/java/com/actiontech/dble/backend/datasource/PhysicalDbGroup.java
index 926616acc8..b82c721ea4 100644
--- a/src/main/java/com/actiontech/dble/backend/datasource/PhysicalDbGroup.java
+++ b/src/main/java/com/actiontech/dble/backend/datasource/PhysicalDbGroup.java
@@ -451,6 +451,10 @@ public String switchMaster(String writeHost, boolean syncWriteConf) {
             }
             newWriteHost.setReadInstance(false);
             writeDbInstance = newWriteHost;
+            if (!newWriteHost.isDisabled() && rwSplitMode == RW_SPLIT_OFF && newWriteHost.isConnectionPoolInActive()) {
+                //need init connection pool
+                newWriteHost.start("ha switch", false);
+            }
             return this.getClusterHaJson();
         } catch (Exception e) {
             LOGGER.warn("switchMaster Exception ", e);
diff --git a/src/main/java/com/actiontech/dble/backend/datasource/PhysicalDbInstance.java b/src/main/java/com/actiontech/dble/backend/datasource/PhysicalDbInstance.java
index 4e398e12fe..76a60f7135 100644
--- a/src/main/java/com/actiontech/dble/backend/datasource/PhysicalDbInstance.java
+++ b/src/main/java/com/actiontech/dble/backend/datasource/PhysicalDbInstance.java
@@ -472,6 +472,10 @@ public final int getTotalConnections() {
         return connectionPool.size() - connectionPool.getCount(PooledConnection.STATE_REMOVED);
     }
 
+    public boolean isConnectionPoolInActive() {
+        return connectionPool.isInActive();
+    }
+
     @Override
     public boolean equals(Object other) {
         if (this == other) {
diff --git a/src/main/java/com/actiontech/dble/backend/pool/ConnectionPool.java b/src/main/java/com/actiontech/dble/backend/pool/ConnectionPool.java
index 2a34e2a440..4e143f6bcf 100644
--- a/src/main/java/com/actiontech/dble/backend/pool/ConnectionPool.java
+++ b/src/main/java/com/actiontech/dble/backend/pool/ConnectionPool.java
@@ -143,7 +143,7 @@ public PooledConnection borrow(final String schema, long timeout, final TimeUnit
     }
 
     private void newPooledEntry(final String schema, final int waiting, boolean createByWaiter) {
-        if (instance.isDisabled() || isClosed.get()) {
+        if (isInActive()) {
             return;
         }
 
@@ -423,6 +423,10 @@ public void startEvictor() {
         }
     }
 
+    public boolean isInActive() {
+        return instance.isDisabled() || isClosed.get();
+    }
+
     /**
      * Stops the evictor.
      */