This is an automated email from the ASF dual-hosted git repository. yiguolei pushed a commit to branch branch-2.1 in repository https://gitbox.apache.org/repos/asf/doris.git
commit bc03354be89b6a189861e7bdf6fc61e617e0194d Author: zy-kkk <[email protected]> AuthorDate: Tue Jan 23 11:52:20 2024 +0800 [improvement](jdbc catalog) Optimize the Close logic of JDBC client (#30236) Optimize the Close logic of the JDBC client so that the Jdbc Catalog can correctly cancel the running query when the query is cancelled. --- .../java/org/apache/doris/jdbc/JdbcExecutor.java | 52 +++++++++++++++------- 1 file changed, 35 insertions(+), 17 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 1129be27833..73773cd1b04 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 @@ -34,6 +34,7 @@ import com.clickhouse.data.value.UnsignedInteger; import com.clickhouse.data.value.UnsignedLong; import com.clickhouse.data.value.UnsignedShort; import com.google.common.base.Preconditions; +import com.google.common.util.concurrent.MoreExecutors; import com.vesoft.nebula.client.graph.data.ValueWrapper; import org.apache.log4j.Logger; import org.apache.thrift.TDeserializer; @@ -124,25 +125,42 @@ public class JdbcExecutor { } public void close() throws Exception { - if (resultSet != null) { - resultSet.close(); - } - if (stmt != null) { - stmt.cancel(); - stmt.close(); - } - if (conn != null) { - conn.close(); + try { + if (stmt != null) { + stmt.cancel(); + } + if (conn != null && resultSet != null) { + abortReadConnection(conn, resultSet, tableType); + } + if (config.getMinIdleSize() == 0) { + // it can be immediately closed if there is no need to maintain the cache of datasource + druidDataSource.close(); + JdbcDataSource.getDataSource().getSourcesMap().clear(); + druidDataSource = null; + } + } finally { + if (stmt != null) { + stmt.close(); + } + if (resultSet != null) { + resultSet.close(); + } + if (conn != null) { + conn.close(); + } + resultSet = null; + stmt = null; + conn = null; } - if (config.getMinIdleSize() == 0) { - // it can be immediately closed if there is no need to maintain the cache of datasource - druidDataSource.close(); - JdbcDataSource.getDataSource().getSourcesMap().clear(); - druidDataSource = null; + } + + 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 driver + // attempts to drain the connection by reading all the results. + connection.abort(MoreExecutors.directExecutor()); } - resultSet = null; - stmt = null; - conn = null; } public int read() throws UdfRuntimeException { --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
