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

zykkk pushed a commit to branch branch-2.0
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-2.0 by this push:
     new f894a6b0a05 [branch-2.0][improvement](jdbc catalog) Force all 
resources to be closed in the close method (#39665)
f894a6b0a05 is described below

commit f894a6b0a05e21830c38f9182f1215094d8d6128
Author: zy-kkk <zhongy...@gmail.com>
AuthorDate: Thu Aug 22 15:23:16 2024 +0800

    [branch-2.0][improvement](jdbc catalog) Force all resources to be closed in 
the close method (#39665)
    
    pick #39423
    
    Force all resources to be closed in the close method. In the previous
    logic, query errors or query cancellation will not force the connection
    to be closed, which will cause abnormal Hikari connection counts.
    Although forced connection closure will generate some error logs in some
    cases, we should have this bottom-line guarantee and refine the closing
    logic later
---
 .../java/org/apache/doris/jdbc/JdbcExecutor.java   | 32 ++++++----------------
 1 file changed, 8 insertions(+), 24 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 17576c5b2cf..a3856a1156c 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
@@ -125,25 +125,18 @@ public class JdbcExecutor {
 
     public void close() throws Exception {
         try {
-            if (stmt != null) {
+            if (stmt != null && !stmt.isClosed()) {
                 try {
                     stmt.cancel();
                 } catch (SQLException e) {
-                    LOG.error("Error cancelling statement", e);
+                    LOG.warn("Cannot cancelling statement: ", 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 no abort operation is performed, the resource needs to be 
closed manually
-            if (!aborted) {
-                closeResources(resultSet, stmt, conn);
+            if (conn != null && resultSet != null) {
+                abortReadConnection(conn, resultSet, tableType);
             }
+            closeResources(resultSet, stmt, conn);
         } finally {
             if (config.getConnectionPoolMinSize() == 0 && hikariDataSource != 
null) {
                 hikariDataSource.close();
@@ -157,32 +150,23 @@ public class JdbcExecutor {
         for (AutoCloseable closeable : closeables) {
             if (closeable != null) {
                 try {
-                    if (closeable instanceof Connection) {
-                        if (!((Connection) closeable).isClosed()) {
-                            closeable.close();
-                        }
-                    } else {
-                        closeable.close();
-                    }
+                    closeable.close();
                 } catch (Exception e) {
-                    LOG.error("Cannot close resource: ", e);
+                    LOG.warn("Cannot close resource: ", e);
                 }
             }
         }
     }
 
-    public boolean abortReadConnection(Connection connection, ResultSet 
resultSet, TOdbcTableType tableType)
+    public void 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/SQLServer driver
             // attempts to drain the connection by reading all the results.
             connection.abort(MoreExecutors.directExecutor());
-            return true;
         }
-        return false;
     }
 
-
     public void cleanDataSource() {
         if (hikariDataSource != null) {
             hikariDataSource.close();


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

Reply via email to