You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by yi...@apache.org on 2024/02/22 12:35:29 UTC
(doris) 03/03: [fix](jdbc catalog) Fix Resource Closing Logic After Connection Abort (#31219)
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 18f9ca92420cf6b61c9972f732912bf8f55569b7
Author: zy-kkk <zh...@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-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org