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 961f590  Add in use flag with backend connection (#10185)
961f590 is described below

commit 961f590ef4be56958ccb02cabd61e3e3c176a58d
Author: Liang Zhang <terrym...@163.com>
AuthorDate: Sat Apr 24 23:40:44 2021 +0800

    Add in use flag with backend connection (#10185)
---
 .../communication/jdbc/connection/BackendConnection.java     |  3 +++
 .../proxy/frontend/command/CommandExecutorTask.java          |  2 ++
 .../proxy/frontend/postgresql/PostgreSQLFrontendEngine.java  | 12 ++++++++++++
 3 files changed, 17 insertions(+)

diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/BackendConnection.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/BackendConnection.java
index 50a5bc3..46d16f41 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/BackendConnection.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/BackendConnection.java
@@ -67,6 +67,9 @@ public final class BackendConnection implements 
ExecutorJDBCManager {
     private volatile int connectionId;
     
     @Setter
+    private volatile boolean inUse;
+    
+    @Setter
     private volatile Grantee grantee;
     
     @Setter
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/command/CommandExecutorTask.java
 
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/command/CommandExecutorTask.java
index e3f048e..1b24d48 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/command/CommandExecutorTask.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/command/CommandExecutorTask.java
@@ -61,6 +61,7 @@ public final class CommandExecutorTask implements Runnable {
      */
     @Override
     public void run() {
+        backendConnection.setInUse(true);
         boolean isNeedFlush = false;
         try (PacketPayload payload = 
databaseProtocolFrontendEngine.getCodecEngine().createPacketPayload((ByteBuf) 
message)) {
             ConnectionStatus connectionStatus = 
backendConnection.getConnectionStatus();
@@ -74,6 +75,7 @@ public final class CommandExecutorTask implements Runnable {
             // CHECKSTYLE:ON
             processException(ex);
         } finally {
+            backendConnection.setInUse(false);
             Collection<SQLException> exceptions = closeExecutionResources();
             if (isNeedFlush) {
                 context.flush();
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/PostgreSQLFrontendEngine.java
 
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/PostgreSQLFrontendEngine.java
index 46a9f81..e3d1542 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/PostgreSQLFrontendEngine.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/PostgreSQLFrontendEngine.java
@@ -46,9 +46,21 @@ public final class PostgreSQLFrontendEngine implements 
DatabaseProtocolFrontendE
     
     @Override
     public void release(final BackendConnection backendConnection) {
+        waitingForFinish(backendConnection);
         
PostgreSQLBinaryStatementRegistry.getInstance().unregister(backendConnection.getConnectionId());
     }
     
+    private void waitingForFinish(final BackendConnection backendConnection) {
+        int tryTimes = 0;
+        while (backendConnection.isInUse() && tryTimes++ < 3) {
+            try {
+                Thread.sleep(500L);
+            } catch (final InterruptedException ex) {
+                Thread.currentThread().interrupt();
+            }
+        }
+    }
+    
     @Override
     public String getDatabaseType() {
         return "PostgreSQL";

Reply via email to