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


##########
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:
   Looking at `e1d7f156`, `close()` is already using 
`AutoCloseables.close(this::closeSession, sqlClient);`, so the `finally` 
exception-masking concern is already resolved. Moving `if 
(!builder.ownsSession) return;` before `getOptions()` is just a minor 
non-blocking nit.



##########
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:
   Considering this non-blocking as well — nice-to-have if you push another 
update, otherwise fine as is.



##########
flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/client/ArrowFlightSqlClientHandler.java:
##########
@@ -291,22 +296,31 @@ 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.isPresent()) {
+        handleBenignCloseException(
+            fre, "Failed to close Flight SQL session.", "closing Flight SQL 
session");
+      } else {
+        // Without a catalog the driver never asked for a session; the server 
may not support them.
+        logSuppressedCloseException(fre, "closing Flight SQL session");

Review Comment:
   Without ignoring these "no session" statuses when `catalog` is unset, 
`Connection#close()` would throw `SQLException` against any Flight SQL server 
that does not implement `CloseSession` (including the default Java/Go/C++/Rust 
implementations) or for unauthenticated connections (such as 
`ConnectionTest#testGetBasicClientNoAuthShouldOpenConnection`). Genuine 
failures like `UNKNOWN`, `DEADLINE_EXCEEDED`, `PERMISSION_DENIED`, or 
non-GOAWAY `INTERNAL` still fail and propagate `SQLException`.



-- 
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