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

dengzh pushed a commit to branch branch-3.1
in repository https://gitbox.apache.org/repos/asf/hive.git


The following commit(s) were added to refs/heads/branch-3.1 by this push:
     new ebe7abbab69 HIVE-26743: backport HIVE-24694: Early connection close to 
release server resources during creating (#3768)
ebe7abbab69 is described below

commit ebe7abbab69a34d0ccdfd143d153f9b1b78deaa1
Author: shalk(xiao kun) <[email protected]>
AuthorDate: Thu Nov 17 16:40:36 2022 +0800

    HIVE-26743: backport HIVE-24694: Early connection close to release server 
resources during creating (#3768)
---
 .../java/org/apache/hive/jdbc/HiveConnection.java  | 71 ++++++++++++++++------
 1 file changed, 53 insertions(+), 18 deletions(-)

diff --git a/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java 
b/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java
index a654b054251..5d013dc336f 100644
--- a/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java
+++ b/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java
@@ -237,6 +237,12 @@ public class HiveConnection implements java.sql.Connection 
{
           LOG.warn("Failed to connect to " + connParams.getHost() + ":" + 
connParams.getPort());
           String errMsg = null;
           String warnMsg = "Could not open client transport with JDBC Uri: " + 
jdbcUriString + ": ";
+          try {
+            close();
+          } catch (Exception ex) {
+            // Swallow the exception
+            LOG.debug("Error while closing the connection", ex);
+          }
           if (ZooKeeperHiveClientHelper.isZkDynamicDiscoveryMode(sessConfMap)) 
{
             errMsg = "Could not open client transport for any of the Server 
URI's in ZooKeeper: ";
             // Try next available server in zookeeper, or retry all the 
servers again if retry is enabled
@@ -267,15 +273,15 @@ public class HiveConnection implements 
java.sql.Connection {
 
   private void executeInitSql() throws SQLException {
     if (initFile != null) {
-      try {
+      try (Statement st = createStatement()) {
         List<String> sqlList = parseInitFile(initFile);
-        Statement st = createStatement();
         for(String sql : sqlList) {
           boolean hasResult = st.execute(sql);
           if (hasResult) {
-            ResultSet rs = st.getResultSet();
-            while (rs.next()) {
-              System.out.println(rs.getString(1));
+            try (ResultSet rs = st.getResultSet()) {
+              while (rs.next()) {
+                System.out.println(rs.getString(1));
+              }
             }
           }
         }
@@ -817,6 +823,9 @@ public class HiveConnection implements java.sql.Connection {
   }
 
   public String getDelegationToken(String owner, String renewer) throws 
SQLException {
+    if (isClosed) {
+      throw new SQLException("Connection is closed");
+    }
     TGetDelegationTokenReq req = new TGetDelegationTokenReq(sessHandle, owner, 
renewer);
     try {
       TGetDelegationTokenResp tokenResp = client.GetDelegationToken(req);
@@ -829,6 +838,9 @@ public class HiveConnection implements java.sql.Connection {
   }
 
   public void cancelDelegationToken(String tokenStr) throws SQLException {
+    if (isClosed) {
+      throw new SQLException("Connection is closed");
+    }
     TCancelDelegationTokenReq cancelReq = new 
TCancelDelegationTokenReq(sessHandle, tokenStr);
     try {
       TCancelDelegationTokenResp cancelResp =
@@ -842,6 +854,9 @@ public class HiveConnection implements java.sql.Connection {
   }
 
   public void renewDelegationToken(String tokenStr) throws SQLException {
+    if (isClosed) {
+      throw new SQLException("Connection is closed");
+    }
     TRenewDelegationTokenReq cancelReq = new 
TRenewDelegationTokenReq(sessHandle, tokenStr);
     try {
       TRenewDelegationTokenResp renewResp =
@@ -873,17 +888,19 @@ public class HiveConnection implements 
java.sql.Connection {
 
   @Override
   public void close() throws SQLException {
-    if (!isClosed) {
-      TCloseSessionReq closeReq = new TCloseSessionReq(sessHandle);
-      try {
+    try {
+      if (!isClosed) {
+        TCloseSessionReq closeReq = new TCloseSessionReq(sessHandle);
         client.CloseSession(closeReq);
-      } catch (TException e) {
-        throw new SQLException("Error while cleaning up the server resources", 
e);
-      } finally {
-        isClosed = true;
-        if (transport != null) {
-          transport.close();
-        }
+      }
+    } catch (TException e) {
+      throw new SQLException("Error while cleaning up the server resources", 
e);
+    } finally {
+      isClosed = true;
+      client = null;
+      if (transport != null && transport.isOpen()) {
+        transport.close();
+        transport = null;
       }
     }
   }
@@ -994,6 +1011,9 @@ public class HiveConnection implements java.sql.Connection 
{
       throw new SQLException("Statement with resultset type " + resultSetType +
           " is not supported", "HYC00"); // Optional feature not implemented
     }
+    if (isClosed) {
+      throw new SQLException("Connection is closed");
+    }
     return new HiveStatement(this, client, sessHandle,
         resultSetType == ResultSet.TYPE_SCROLL_INSENSITIVE, fetchSize);
   }
@@ -1180,6 +1200,9 @@ public class HiveConnection implements 
java.sql.Connection {
     if (timeout < 0) {
       throw new SQLException("timeout value was negative");
     }
+    if (isClosed) {
+      return false;
+    }
     boolean rc = false;
     try {
       String productName = new HiveDatabaseMetaData(this, client, sessHandle)
@@ -1249,6 +1272,9 @@ public class HiveConnection implements 
java.sql.Connection {
 
   @Override
   public PreparedStatement prepareStatement(String sql) throws SQLException {
+    if (isClosed) {
+      throw new SQLException("Connection is closed");
+    }
     return new HivePreparedStatement(this, client, sessHandle, sql);
   }
 
@@ -1261,6 +1287,9 @@ public class HiveConnection implements 
java.sql.Connection {
   @Override
   public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
       throws SQLException {
+    if (isClosed) {
+      throw new SQLException("Connection is closed");
+    }
     return new HivePreparedStatement(this, client, sessHandle, sql);
   }
 
@@ -1300,6 +1329,9 @@ public class HiveConnection implements 
java.sql.Connection {
   @Override
   public PreparedStatement prepareStatement(String sql, int resultSetType,
       int resultSetConcurrency) throws SQLException {
+    if (isClosed) {
+      throw new SQLException("Connection is closed");
+    }
     return new HivePreparedStatement(this, client, sessHandle, sql);
   }
 
@@ -1419,6 +1451,9 @@ public class HiveConnection implements 
java.sql.Connection {
 
 
   private void sendClientInfo() throws SQLClientInfoException {
+    if (isClosed) {
+      throw new SQLClientInfoException("Connection is closed", null);
+    }
     TSetClientInfoReq req = new TSetClientInfoReq(sessHandle);
     Map<String, String> map = new HashMap<>();
     if (clientInfo != null) {
@@ -1505,9 +1540,9 @@ public class HiveConnection implements 
java.sql.Connection {
     if (schema == null || schema.isEmpty()) {
       throw new SQLException("Schema name is null or empty");
     }
-    Statement stmt = createStatement();
-    stmt.execute("use " + schema);
-    stmt.close();
+    try (Statement stmt = createStatement()) {
+      stmt.execute("use " + schema);
+    }
   }
 
   /*

Reply via email to