jbonofre commented on code in PR #1504:
URL: https://github.com/apache/arrow-java/pull/1504#discussion_r4691891417


##########
flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/client/ArrowFlightSqlClientHandlerTest.java:
##########
@@ -145,6 +148,46 @@ public void testCloseHandlesFlightRuntimeException(
     } else {
       assertThrows(SQLException.class, sqlClientHandler::close);
     }
+    verify(sqlClient).close();
+  }
+
+  @ParameterizedTest
+  @MethodSource
+  public void testCloseSessionWithoutCatalog(FlightStatusCode code, boolean 
shouldSuppress)
+      throws Exception {
+    FlightSqlClient sqlClient = mock(FlightSqlClient.class);
+    doThrow(new CallStatus(code, null, null, null).toRuntimeException())
+        .when(sqlClient)
+        .closeSession(any(CloseSessionRequest.class), any(CallOption[].class));
+    ArrowFlightSqlClientHandler sqlClientHandler =
+        new ArrowFlightSqlClientHandler(
+            "cacheKey",
+            sqlClient,
+            new ArrowFlightSqlClientHandler.Builder(),
+            new ArrayList<>(),
+            Optional.empty(),
+            null);
+
+    if (shouldSuppress) {
+      assertDoesNotThrow(sqlClientHandler::close);
+    } else {
+      assertThrows(SQLException.class, sqlClientHandler::close);
+    }
+    verify(sqlClient)
+        .closeSession(
+            any(CloseSessionRequest.class), 
eq(ArrowFlightSqlClientHandler.CLOSE_SESSION_TIMEOUT));
+    verify(sqlClient).close();
+  }
+
+  private static Object[] testCloseSessionWithoutCatalog() {
+    return new Object[] {
+      new Object[] {FlightStatusCode.UNIMPLEMENTED, true},
+      new Object[] {FlightStatusCode.INVALID_ARGUMENT, true},
+      new Object[] {FlightStatusCode.NOT_FOUND, true},
+      new Object[] {FlightStatusCode.UNAVAILABLE, true},
+      new Object[] {FlightStatusCode.UNAUTHENTICATED, true},
+      new Object[] {FlightStatusCode.UNKNOWN, false},
+    };

Review Comment:
   Could we also include `FlightStatusCode.DEADLINE_EXCEEDED` (`false`) here? 
Since `CLOSE_SESSION_TIMEOUT` introduces a 5s deadline on `closeSession`, 
having an explicit test case confirms that a timeout when closing without a 
catalog still closes `sqlClient` and propagates `SQLException`.



##########
flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/client/ArrowFlightSqlClientHandler.java:
##########
@@ -291,22 +306,30 @@ public long executeUpdate(final String query) {
 
   @Override
   public void close() throws SQLException {
-    if (catalog.isPresent()) {
+    final CallOption[] baseOptions = getOptions();
+    final CallOption[] closeSessionOptions = Arrays.copyOf(baseOptions, 
baseOptions.length + 1);
+    closeSessionOptions[baseOptions.length] = CLOSE_SESSION_TIMEOUT;
+    try {
+      if (builder.ownsSession) {
+        sqlClient.closeSession(new CloseSessionRequest(), closeSessionOptions);
+      }
+    } catch (FlightRuntimeException fre) {
+      if (catalog.isEmpty() && 
NO_SESSION_STATUS_CODES.contains(fre.status().code())) {
+        logSuppressedCloseException(fre, "closing Flight SQL session");
+      } else {
+        handleBenignCloseException(
+            fre, "Failed to close Flight SQL session.", "closing Flight SQL 
session");
+      }
+    } finally {
       try {
-        sqlClient.closeSession(new CloseSessionRequest(), getOptions());
+        AutoCloseables.close(sqlClient);
       } catch (FlightRuntimeException fre) {
         handleBenignCloseException(
-            fre, "Failed to close Flight SQL session.", "closing Flight SQL 
session");
+            fre, "Failed to clean up client resources.", "closing Flight SQL 
client");
+      } catch (final Exception e) {
+        throw new SQLException("Failed to clean up client resources.", e);

Review Comment:
   On `main` (via #1268 ), `AutoCloseables.close(this::closeCatalogSession, 
sqlClient)` already closes `sqlClient` when `closeSession` throws, and 
preserves the primary `SQLException` by attaching any secondary cleanup failure 
with `addSuppressed`.
   
   With this `try/catch/finally` rewrite, if `closeSession` throws a 
non-suppressed `SQLException` and `AutoCloseables.close(sqlClient)` in 
`finally` also throws a non-benign exception, the exception from `finally` will 
silently mask the session-close `SQLException`. Also, `baseOptions` / 
`closeSessionOptions` don't need to be allocated when `!builder.ownsSession`.
   
   Could we keep the `AutoCloseables.close(this::closeSession, sqlClient)` 
pattern from `main`?
   
   For example:
   
   ```java
        @Override
        public void close() throws SQLException {
          try {
            AutoCloseables.close(this::closeSession, sqlClient);
          } catch (FlightRuntimeException fre) {
            handleBenignCloseException(
                fre, "Failed to clean up client resources.", "closing Flight 
SQL client");
          } catch (SQLException e) {
            throw e;
          } catch (final Exception e) {
            throw new SQLException("Failed to clean up client resources.", e);
          }
        }
   
        private void closeSession() throws SQLException {
          if (!builder.ownsSession) {
            return;
          }
          final CallOption[] baseOptions = getOptions();
          final CallOption[] closeSessionOptions = Arrays.copyOf(baseOptions, 
baseOptions.length + 1);
          closeSessionOptions[baseOptions.length] = CLOSE_SESSION_TIMEOUT;
          try {
            sqlClient.closeSession(new CloseSessionRequest(), 
closeSessionOptions);
          } catch (FlightRuntimeException fre) {
            if (catalog.isEmpty() && 
NO_SESSION_STATUS_CODES.contains(fre.status().code())) {
              logSuppressedCloseException(fre, "closing Flight SQL session");
            } else {
              handleBenignCloseException(
                  fre, "Failed to close Flight SQL session.", "closing Flight 
SQL session");
            }
          }
        }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to