From 5e9c85e0fc43f4cedf73abdbf7d1134de4b6e817 Mon Sep 17 00:00:00 2001 From: redkale Date: Tue, 18 Apr 2023 22:30:25 +0800 Subject: [PATCH] =?UTF-8?q?DataJdbcSource=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/redkale/net/client/Client.java | 4 ++ .../org/redkale/source/DataJdbcSource.java | 60 ++++++++----------- 2 files changed, 28 insertions(+), 36 deletions(-) diff --git a/src/main/java/org/redkale/net/client/Client.java b/src/main/java/org/redkale/net/client/Client.java index cb1bdf581..26b333fdf 100644 --- a/src/main/java/org/redkale/net/client/Client.java +++ b/src/main/java/org/redkale/net/client/Client.java @@ -371,6 +371,10 @@ public abstract class Client, R extends ClientR return name; } + public int getMaxConns() { + return connLimit; + } + public int getReadTimeoutSeconds() { return readTimeoutSeconds; } diff --git a/src/main/java/org/redkale/source/DataJdbcSource.java b/src/main/java/org/redkale/source/DataJdbcSource.java index b8064cebb..e77260c34 100644 --- a/src/main/java/org/redkale/source/DataJdbcSource.java +++ b/src/main/java/org/redkale/source/DataJdbcSource.java @@ -2786,30 +2786,15 @@ public class DataJdbcSource extends AbstractDataSqlSource { this.connectAttrs.put("user", newUser); this.connectAttrs.put("password", newPassword); if (newMaxconns != this.maxConns) { - ArrayBlockingQueue newQueue = new ArrayBlockingQueue<>(newMaxconns); - ArrayBlockingQueue oldQueue = this.queue; - Semaphore oldSemaphore = this.canNewSemaphore; - this.queue = newQueue; - this.maxConns = newMaxconns; - this.canNewSemaphore = new Semaphore(this.maxConns); - Connection c; - while ((c = oldQueue.poll()) != null) { - try { - if (c.getClientInfo() != null) { - c.getClientInfo().put("version", "-1"); - } - } catch (SQLException e) { - } - offerConnection(c, oldSemaphore); - } + changeMaxConns(newMaxconns); } } private void resetMaxConnection() { if ("mysql".equals(dbtype()) || "postgresql".equals(dbtype())) { int newMaxconns = this.maxConns; - Connection conn = pollConnection(); try { + Connection conn = driver.connect(url, connectAttrs); Statement stmt = conn.createStatement(); if ("mysql".equals(dbtype())) { ResultSet rs = stmt.executeQuery("SHOW VARIABLES LIKE 'max_connections'"); @@ -2823,31 +2808,34 @@ public class DataJdbcSource extends AbstractDataSqlSource { } } stmt.close(); - } catch (SQLException e) { - } finally { - offerConnection(conn); + conn.close(); + } catch (Exception e) { } if (this.maxConns > newMaxconns) { //配置连接数过大 - ArrayBlockingQueue newQueue = new ArrayBlockingQueue<>(newMaxconns); - ArrayBlockingQueue oldQueue = this.queue; - Semaphore oldSemaphore = this.canNewSemaphore; - this.queue = newQueue; - this.maxConns = newMaxconns; - this.canNewSemaphore = new Semaphore(this.maxConns); - Connection c; - while ((c = oldQueue.poll()) != null) { - try { - if (c.getClientInfo() != null) { - c.getClientInfo().put("version", "-1"); - } - } catch (SQLException e) { - } - offerConnection(c, oldSemaphore); - } + changeMaxConns(newMaxconns); } } } + private void changeMaxConns(int newMaxconns) { + ArrayBlockingQueue newQueue = new ArrayBlockingQueue<>(newMaxconns); + ArrayBlockingQueue oldQueue = this.queue; + Semaphore oldSemaphore = this.canNewSemaphore; + this.queue = newQueue; + this.maxConns = newMaxconns; + this.canNewSemaphore = new Semaphore(this.maxConns); + Connection c; + while ((c = oldQueue.poll()) != null) { + try { + if (c.getClientInfo() != null) { + c.getClientInfo().put("version", "-1"); + } + } catch (SQLException e) { + } + offerConnection(c, oldSemaphore); + } + } + public Connection pollConnection() { Connection conn = queue.poll(); if (conn == null) {