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

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


The following commit(s) were added to refs/heads/master by this push:
     new 4cfd9ee67f4 [fix](jdbc catalog) Fix Resource Closing Logic After 
Connection Abort (#31219)
4cfd9ee67f4 is described below

commit 4cfd9ee67f4d8827ac5dfcc4cd6822ddc2065e1b
Author: zy-kkk <zhongy...@gmail.com>
AuthorDate: Thu Feb 22 20:34:09 2024 +0800

    [fix](jdbc catalog) Fix Resource Closing Logic After Connection Abort 
(#31219)
    
    * [improvement](jdbc catalog) Optimize the closing logic of Jdbc connection 
after abort
    
    * [improvement](jdbc catalog) Optimize the closing logic of Jdbc connection 
after abort
    
    * fix
---
 .../java/org/apache/doris/jdbc/JdbcExecutor.java   | 60 ++++++++++++----------
 1 file changed, 34 insertions(+), 26 deletions(-)

diff --git 
a/fe/be-java-extensions/jdbc-scanner/src/main/java/org/apache/doris/jdbc/JdbcExecutor.java
 
b/fe/be-java-extensions/jdbc-scanner/src/main/java/org/apache/doris/jdbc/JdbcExecutor.java
index 8c684219af3..3f00643d126 100644
--- 
a/fe/be-java-extensions/jdbc-scanner/src/main/java/org/apache/doris/jdbc/JdbcExecutor.java
+++ 
b/fe/be-java-extensions/jdbc-scanner/src/main/java/org/apache/doris/jdbc/JdbcExecutor.java
@@ -134,32 +134,20 @@ public class JdbcExecutor {
                     LOG.error("Error cancelling statement", e);
                 }
             }
-            if (conn != null && resultSet != null) {
-                abortReadConnection(conn, resultSet, tableType);
-                try {
-                    resultSet.close();
-                } catch (SQLException e) {
-                    LOG.error("Error closing resultSet", e);
-                }
-                try {
-                    stmt.close();
-                } catch (SQLException e) {
-                    LOG.error("Error closing statement", e);
-                }
-            }
-        } finally {
-            if (conn != null && !conn.isClosed()) {
-                try {
-                    conn.close();
-                } catch (SQLException e) {
-                    LOG.error("Error closing connection", e);
-                }
+
+            boolean shouldAbort = conn != null && resultSet != null
+                    && (tableType == TOdbcTableType.MYSQL || tableType == 
TOdbcTableType.SQLSERVER);
+            boolean aborted = false; // Used to record whether the abort 
operation is performed
+            if (shouldAbort) {
+                aborted = abortReadConnection(conn, resultSet, tableType);
             }
-        }
 
-        if (config.getConnectionPoolMinSize() == 0) {
-            // Close and remove the datasource if necessary
-            if (druidDataSource != null) {
+            // If no abort operation is performed, the resource needs to be 
closed manually
+            if (!aborted) {
+                closeResources(resultSet, stmt, conn);
+            }
+        } finally {
+            if (config.getConnectionPoolMinSize() == 0 && druidDataSource != 
null) {
                 druidDataSource.close();
                 
JdbcDataSource.getDataSource().getSourcesMap().remove(config.createCacheKey());
                 druidDataSource = null;
@@ -167,13 +155,33 @@ public class JdbcExecutor {
         }
     }
 
-    public void abortReadConnection(Connection connection, ResultSet 
resultSet, TOdbcTableType tableType)
+    private void closeResources(AutoCloseable... closeables) {
+        for (AutoCloseable closeable : closeables) {
+            if (closeable != null) {
+                try {
+                    if (closeable instanceof Connection) {
+                        if (!((Connection) closeable).isClosed()) {
+                            closeable.close();
+                        }
+                    } else {
+                        closeable.close();
+                    }
+                } catch (Exception e) {
+                    LOG.error("Cannot close resource: ", e);
+                }
+            }
+        }
+    }
+
+    public boolean abortReadConnection(Connection connection, ResultSet 
resultSet, TOdbcTableType tableType)
             throws SQLException {
         if (!resultSet.isAfterLast() && (tableType == TOdbcTableType.MYSQL || 
tableType == TOdbcTableType.SQLSERVER)) {
-            // Abort connection before closing. Without this, the MySQL driver
+            // Abort connection before closing. Without this, the 
MySQL/SQLServer driver
             // attempts to drain the connection by reading all the results.
             connection.abort(MoreExecutors.directExecutor());
+            return true;
         }
+        return false;
     }
 
     public int read() throws UdfRuntimeException {


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to