You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by wu...@apache.org on 2022/06/28 08:10:26 UTC

[shardingsphere] branch master updated: Fix risk of connection leak in ShardingSphere-Proxy (#18644)

This is an automated email from the ASF dual-hosted git repository.

wuweijie pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 96fbde7b977 Fix risk of connection leak in ShardingSphere-Proxy (#18644)
96fbde7b977 is described below

commit 96fbde7b977df1f9201b99207f73c39a44347629
Author: sora <99...@users.noreply.github.com>
AuthorDate: Tue Jun 28 16:10:19 2022 +0800

    Fix risk of connection leak in ShardingSphere-Proxy (#18644)
    
    * fix -hikari connection leak
    
    * remove the redundant this & the empty line
---
 .../communication/jdbc/connection/JDBCBackendConnection.java      | 8 ++++++++
 .../proxy/backend/communication/vertx/VertxBackendConnection.java | 7 +++++++
 2 files changed, 15 insertions(+)

diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/JDBCBackendConnection.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/JDBCBackendConnection.java
index 569de746702..efa51065178 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/JDBCBackendConnection.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/JDBCBackendConnection.java
@@ -20,6 +20,7 @@ package org.apache.shardingsphere.proxy.backend.communication.jdbc.connection;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.LinkedHashMultimap;
 import com.google.common.collect.Multimap;
+import java.util.concurrent.atomic.AtomicBoolean;
 import lombok.Getter;
 import lombok.Setter;
 import org.apache.shardingsphere.infra.executor.sql.execute.engine.ConnectionMode;
@@ -64,10 +65,13 @@ public final class JDBCBackendConnection implements BackendConnection<Void>, Exe
     
     private final ResourceLock resourceLock = new ResourceLock();
     
+    private final AtomicBoolean closed;
+    
     private volatile int connectionReferenceCount;
     
     public JDBCBackendConnection(final ConnectionSession connectionSession) {
         this.connectionSession = connectionSession;
+        closed = new AtomicBoolean(false);
     }
     
     @Override
@@ -203,6 +207,9 @@ public final class JDBCBackendConnection implements BackendConnection<Void>, Exe
             if (!connectionSession.getTransactionStatus().isInConnectionHeldTransaction()) {
                 result.addAll(closeDatabaseCommunicationEngines(true));
                 result.addAll(closeConnections(false));
+            } else if (closed.get()) {
+                result.addAll(closeDatabaseCommunicationEngines(true));
+                result.addAll(closeConnections(true));
             }
             if (result.isEmpty()) {
                 return null;
@@ -214,6 +221,7 @@ public final class JDBCBackendConnection implements BackendConnection<Void>, Exe
     @Override
     public Void closeAllResources() {
         synchronized (this) {
+            closed.set(true);
             closeDatabaseCommunicationEngines(true);
             closeConnections(true);
             closeFederationExecutor();
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/vertx/VertxBackendConnection.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/vertx/VertxBackendConnection.java
index 38a40ed3569..634cdbb732e 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/vertx/VertxBackendConnection.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/vertx/VertxBackendConnection.java
@@ -24,6 +24,7 @@ import io.vertx.core.CompositeFuture;
 import io.vertx.core.Future;
 import io.vertx.sqlclient.SqlClient;
 import io.vertx.sqlclient.SqlConnection;
+import java.util.concurrent.atomic.AtomicBoolean;
 import lombok.Getter;
 import org.apache.shardingsphere.infra.executor.sql.execute.engine.ConnectionMode;
 import org.apache.shardingsphere.infra.executor.sql.prepare.driver.vertx.ExecutorVertxConnectionManager;
@@ -52,10 +53,13 @@ public final class VertxBackendConnection implements BackendConnection<Future<Vo
     
     private final Multimap<String, Future<SqlConnection>> cachedConnections = LinkedHashMultimap.create();
     
+    private final AtomicBoolean closed;
+    
     public VertxBackendConnection(final ConnectionSession connectionSession) {
         if (TransactionType.LOCAL != connectionSession.getTransactionStatus().getTransactionType()) {
             throw new UnsupportedOperationException("Vert.x backend supports LOCAL transaction only for now.");
         }
+        closed = new AtomicBoolean(false);
         this.connectionSession = connectionSession;
     }
     
@@ -131,12 +135,15 @@ public final class VertxBackendConnection implements BackendConnection<Future<Vo
     public Future<Void> closeExecutionResources() {
         if (!connectionSession.getTransactionStatus().isInTransaction()) {
             return closeAllConnections(false);
+        } else if (closed.get()) {
+            return closeAllConnections(true);
         }
         return Future.succeededFuture();
     }
     
     @Override
     public Future<Void> closeAllResources() {
+        closed.set(true);
         return closeAllConnections(true);
     }