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

jbonofre pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-java.git


The following commit(s) were added to refs/heads/main by this push:
     new cd29f1b56 GH-1502: [JDBC] Close the server session on Connection#close 
without a catalog (#1504)
cd29f1b56 is described below

commit cd29f1b5689fbaada0937953231bfb4f2f84438c
Author: Abhishek Pathania <[email protected]>
AuthorDate: Thu Nov 26 14:29:03 2026 +0530

    GH-1502: [JDBC] Close the server session on Connection#close without a 
catalog (#1504)
    
    ## What's Changed
    
    `Connection#close` now sends `CloseSession` even when no `catalog` was
    set, so servers that create a session per connection can free it right
    away.
    
    - Without a catalog, statuses that mean there is no session to close
    (`UNIMPLEMENTED`, `INVALID_ARGUMENT`, `NOT_FOUND`, `UNAUTHENTICATED`)
    are logged at debug and ignored. Other errors are handled as with a
    catalog.
    - The call has a 5 second deadline, so `close` cannot hang on an
    unresponsive server. `FlightClient#close` already waits up to 5 seconds
    for the channel to shut down.
    - The gRPC channel is now closed even when `CloseSession` throws.
    - Handlers that `getStreams` builds for other endpoint locations never
    send `CloseSession`. They authenticate with the connection's
    credentials, so on a server that keys sessions by bearer token, closing
    one after a failed fetch would end the connection's own session. ADBC's
    driver also closes only the main client's session.
    
    This matches ADBC's Flight SQL driver, which always sends `CloseSession`
    on close.
    
    The code and tests were drafted with an AI assistant (Claude). I
    reviewed every change and also ran it against our own Flight SQL server,
    where 25 reconnects left 25 sessions open before this change and none
    after.
    
    Closes #1502.
---
 .../jdbc/client/ArrowFlightSqlClientHandler.java   | 41 ++++++++++++---
 .../apache/arrow/driver/jdbc/ConnectionTest.java   | 21 ++++++++
 .../apache/arrow/driver/jdbc/ResultSetTest.java    | 56 ++++++++++++++++++++
 .../client/ArrowFlightSqlClientHandlerTest.java    | 61 ++++++++++++++++++++++
 4 files changed, 173 insertions(+), 6 deletions(-)

diff --git 
a/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/client/ArrowFlightSqlClientHandler.java
 
b/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/client/ArrowFlightSqlClientHandler.java
index 9c168126a..58209cd90 100644
--- 
a/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/client/ArrowFlightSqlClientHandler.java
+++ 
b/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/client/ArrowFlightSqlClientHandler.java
@@ -28,11 +28,13 @@ import java.time.Duration;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.EnumSet;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
 import java.util.Set;
+import java.util.concurrent.TimeUnit;
 import org.apache.arrow.driver.jdbc.client.oauth.OAuthConfiguration;
 import org.apache.arrow.driver.jdbc.client.oauth.OAuthCredentialWriter;
 import org.apache.arrow.driver.jdbc.client.oauth.OAuthTokenProvider;
@@ -40,6 +42,7 @@ import 
org.apache.arrow.driver.jdbc.client.utils.ClientAuthenticationUtils;
 import org.apache.arrow.driver.jdbc.client.utils.FlightClientCache;
 import org.apache.arrow.driver.jdbc.client.utils.FlightLocationQueue;
 import org.apache.arrow.flight.CallOption;
+import org.apache.arrow.flight.CallOptions;
 import org.apache.arrow.flight.CallStatus;
 import org.apache.arrow.flight.CloseSessionRequest;
 import org.apache.arrow.flight.FlightClient;
@@ -81,6 +84,18 @@ public final class ArrowFlightSqlClientHandler implements 
AutoCloseable {
   // JDBC connection string query parameter
   private static final String CATALOG = "catalog";
 
+  @VisibleForTesting
+  static final CallOption CLOSE_SESSION_TIMEOUT = CallOptions.timeout(5, 
TimeUnit.SECONDS);
+
+  // No session to close: CloseSession is unsupported or unknown, none was 
created, or the
+  // client is not (or no longer) authenticated.
+  private static final Set<FlightStatusCode> NO_SESSION_STATUS_CODES =
+      EnumSet.of(
+          FlightStatusCode.UNIMPLEMENTED,
+          FlightStatusCode.INVALID_ARGUMENT,
+          FlightStatusCode.NOT_FOUND,
+          FlightStatusCode.UNAUTHENTICATED);
+
   private final String cacheKey;
   private final FlightSqlClient sqlClient;
   private final Set<CallOption> options = new HashSet<>();
@@ -293,7 +308,7 @@ public final class ArrowFlightSqlClientHandler implements 
AutoCloseable {
   @Override
   public void close() throws SQLException {
     try {
-      AutoCloseables.close(this::closeCatalogSession, sqlClient);
+      AutoCloseables.close(this::closeSession, sqlClient);
     } catch (FlightRuntimeException fre) {
       handleBenignCloseException(
           fre, "Failed to clean up client resources.", "closing Flight SQL 
client");
@@ -304,11 +319,19 @@ public final class ArrowFlightSqlClientHandler implements 
AutoCloseable {
     }
   }
 
-  private void closeCatalogSession() throws SQLException {
-    if (catalog.isPresent()) {
-      try {
-        sqlClient.closeSession(new CloseSessionRequest(), getOptions());
-      } catch (FlightRuntimeException fre) {
+  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");
       }
@@ -723,6 +746,11 @@ public final class ArrowFlightSqlClientHandler implements 
AutoCloseable {
 
     @VisibleForTesting Optional<String> catalog = Optional.empty();
 
+    // Copies (getStreams endpoint handlers) authenticate as the connection; 
on servers that key
+    // sessions by token, closing one would end the connection's session. With 
retainAuth=false a
+    // copy's own session is left to expire.
+    @VisibleForTesting boolean ownsSession = true;
+
     @VisibleForTesting @Nullable FlightClientCache flightClientCache;
 
     @VisibleForTesting @Nullable Duration connectTimeout;
@@ -770,6 +798,7 @@ public final class ArrowFlightSqlClientHandler implements 
AutoCloseable {
       this.allocator = original.allocator;
       this.catalog = original.catalog;
       this.oauthConfig = original.oauthConfig;
+      this.ownsSession = false;
 
       if (original.retainCookies) {
         this.cookieFactory = original.cookieFactory;
diff --git 
a/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ConnectionTest.java
 
b/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ConnectionTest.java
index ca9ae1adf..eda38d85e 100644
--- 
a/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ConnectionTest.java
+++ 
b/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ConnectionTest.java
@@ -650,6 +650,27 @@ public class ConnectionTest {
     }
   }
 
+  @Test
+  public void testCloseWithoutCatalogClosesSession() throws Exception {
+    final Properties properties = new Properties();
+    properties.put(ArrowFlightConnectionProperty.USER.camelName(), userTest);
+    properties.put(ArrowFlightConnectionProperty.PASSWORD.camelName(), 
passTest);
+    properties.put("useEncryption", false);
+    final int closeSessionCountBefore = PRODUCER.getCloseSessionCount();
+
+    try (Connection connection =
+        DriverManager.getConnection(
+            "jdbc:arrow-flight-sql://"
+                + FLIGHT_SERVER_TEST_EXTENSION.getHost()
+                + ":"
+                + FLIGHT_SERVER_TEST_EXTENSION.getPort(),
+            properties)) {
+      assertTrue(connection.isValid(300));
+    }
+
+    assertEquals(closeSessionCountBefore + 1, PRODUCER.getCloseSessionCount());
+  }
+
   @Test
   public void testSetCatalogShouldUpdateSessionOptions() throws Exception {
     final Properties properties = new Properties();
diff --git 
a/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ResultSetTest.java
 
b/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ResultSetTest.java
index 7d1bf67e4..c5c6a4fbb 100644
--- 
a/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ResultSetTest.java
+++ 
b/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ResultSetTest.java
@@ -48,15 +48,19 @@ import java.util.Random;
 import java.util.Set;
 import java.util.UUID;
 import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.atomic.AtomicInteger;
 import org.apache.arrow.driver.jdbc.utils.CoreMockedSqlProducers;
 import org.apache.arrow.driver.jdbc.utils.FallbackFlightSqlProducer;
 import org.apache.arrow.driver.jdbc.utils.PartitionedFlightSqlProducer;
+import org.apache.arrow.flight.Action;
+import org.apache.arrow.flight.FlightConstants;
 import org.apache.arrow.flight.FlightEndpoint;
 import org.apache.arrow.flight.FlightProducer;
 import org.apache.arrow.flight.FlightRuntimeException;
 import org.apache.arrow.flight.FlightServer;
 import org.apache.arrow.flight.FlightStatusCode;
 import org.apache.arrow.flight.Location;
+import org.apache.arrow.flight.Result;
 import org.apache.arrow.flight.Ticket;
 import org.apache.arrow.memory.BufferAllocator;
 import org.apache.arrow.memory.RootAllocator;
@@ -559,6 +563,58 @@ public class ResultSetTest {
     }
   }
 
+  @Test
+  public void testEndpointFailureDoesNotCloseSession() throws Exception {
+    final Schema schema =
+        new Schema(
+            Collections.singletonList(
+                Field.nullablePrimitive("int_column", new ArrowType.Int(32, 
true))));
+    final AtomicInteger closeSessionCount = new AtomicInteger();
+    final FlightProducer dataProducer =
+        new PartitionedFlightSqlProducer.DataOnlyFlightSqlProducer(
+            new Ticket("first".getBytes(StandardCharsets.UTF_8)), null) {
+          @Override
+          public void doAction(
+              CallContext context, Action action, StreamListener<Result> 
listener) {
+            if 
(action.getType().equals(FlightConstants.CLOSE_SESSION.getType())) {
+              closeSessionCount.incrementAndGet();
+            }
+            super.doAction(context, action, listener);
+          }
+        };
+    try (BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE);
+        FlightServer dataServer =
+            FlightServer.builder(allocator, forGrpcInsecure("localhost", 0), 
dataProducer)
+                .build()
+                .start()) {
+      final FlightEndpoint endpoint =
+          new FlightEndpoint(
+              new Ticket("wrong".getBytes(StandardCharsets.UTF_8)), 
dataServer.getLocation());
+
+      try (final PartitionedFlightSqlProducer rootProducer =
+              new PartitionedFlightSqlProducer(schema, endpoint);
+          FlightServer rootServer =
+              FlightServer.builder(allocator, forGrpcInsecure("localhost", 0), 
rootProducer)
+                  .build()
+                  .start();
+          Connection newConnection =
+              DriverManager.getConnection(
+                  String.format(
+                      "jdbc:arrow-flight-sql://%s:%d/?useEncryption=false",
+                      rootServer.getLocation().getUri().getHost(), 
rootServer.getPort()));
+          Statement newStatement = newConnection.createStatement()) {
+        assertThrows(
+            SQLException.class,
+            () -> {
+              ResultSet result = newStatement.executeQuery("Select 
partitioned_data");
+              while (result.next()) {}
+            });
+      }
+
+      assertEquals(0, closeSessionCount.get());
+    }
+  }
+
   @Test
   public void testPartitionedFlightServerAllFailure() throws Exception {
     // Arrange
diff --git 
a/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/client/ArrowFlightSqlClientHandlerTest.java
 
b/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/client/ArrowFlightSqlClientHandlerTest.java
index 4d37b9110..a885c20be 100644
--- 
a/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/client/ArrowFlightSqlClientHandlerTest.java
+++ 
b/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/client/ArrowFlightSqlClientHandlerTest.java
@@ -22,8 +22,10 @@ import static org.junit.jupiter.api.Assertions.assertSame;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.verifyNoInteractions;
 import static org.mockito.Mockito.when;
@@ -149,6 +151,46 @@ public class ArrowFlightSqlClientHandlerTest {
     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},
+      new Object[] {FlightStatusCode.TIMED_OUT, false},
+    };
+  }
+
   private static Object[] testCloseHandlesFlightRuntimeException() {
     CallStatus benignInternalError =
         new CallStatus(FlightStatusCode.INTERNAL, null, "Connection closed 
after GOAWAY", null);
@@ -168,4 +210,23 @@ public class ArrowFlightSqlClientHandlerTest {
       new Object[] {true, CallStatus.CANCELLED, false},
     };
   }
+
+  @Test
+  public void testCloseFromBuilderCopyDoesNotCloseSession() throws Exception {
+    FlightSqlClient sqlClient = mock(FlightSqlClient.class);
+    ArrowFlightSqlClientHandler sqlClientHandler =
+        new ArrowFlightSqlClientHandler(
+            "cacheKey",
+            sqlClient,
+            new ArrowFlightSqlClientHandler.Builder(new 
ArrowFlightSqlClientHandler.Builder()),
+            new ArrayList<>(),
+            Optional.of("test_catalog"),
+            null);
+
+    sqlClientHandler.close();
+
+    verify(sqlClient, never())
+        .closeSession(any(CloseSessionRequest.class), any(CallOption[].class));
+    verify(sqlClient).close();
+  }
 }

Reply via email to