From a17fb4c7e5c7a0333f43f3d6ee72281466721834 Mon Sep 17 00:00:00 2001 From: AbhishekPathania Date: Wed, 18 Nov 2026 19:41:09 +0530 Subject: [PATCH 1/3] GH-1502: [JDBC] Close the server session on Connection#close without a catalog --- .../client/ArrowFlightSqlClientHandler.java | 34 +++++++++++++------ .../arrow/driver/jdbc/ConnectionTest.java | 21 ++++++++++++ .../ArrowFlightSqlClientHandlerTest.java | 25 ++++++++++++++ 3 files changed, 69 insertions(+), 11 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 97108e599b..221bbc9bd0 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 @@ -32,6 +32,7 @@ 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; @@ -39,6 +40,7 @@ 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; @@ -80,6 +82,9 @@ 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); + private final String cacheKey; private final FlightSqlClient sqlClient; private final Set options = new HashSet<>(); @@ -291,22 +296,29 @@ 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 { + 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"); + } + } 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); } } - try { - AutoCloseables.close(sqlClient); - } catch (FlightRuntimeException fre) { - handleBenignCloseException( - 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); - } } /** 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 ca9ae1adf3..eda38d85ef 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 void testJdbcDriverVersionIntegration() throws Exception { } } + @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/client/ArrowFlightSqlClientHandlerTest.java b/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/client/ArrowFlightSqlClientHandlerTest.java index b44b310eb5..47b29e1a83 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.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.verify; import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.when; @@ -145,6 +147,29 @@ public void testCloseHandlesFlightRuntimeException( } else { assertThrows(SQLException.class, sqlClientHandler::close); } + verify(sqlClient).close(); + } + + @Test + public void testCloseSessionWithoutCatalogIsBestEffortWithTimeout() throws Exception { + FlightSqlClient sqlClient = mock(FlightSqlClient.class); + doThrow(CallStatus.UNKNOWN.toRuntimeException()) + .when(sqlClient) + .closeSession(any(CloseSessionRequest.class), any(CallOption[].class)); + ArrowFlightSqlClientHandler sqlClientHandler = + new ArrowFlightSqlClientHandler( + "cacheKey", + sqlClient, + new ArrowFlightSqlClientHandler.Builder(), + new ArrayList<>(), + Optional.empty(), + null); + + assertDoesNotThrow(sqlClientHandler::close); + verify(sqlClient) + .closeSession( + any(CloseSessionRequest.class), eq(ArrowFlightSqlClientHandler.CLOSE_SESSION_TIMEOUT)); + verify(sqlClient).close(); } private static Object[] testCloseHandlesFlightRuntimeException() { From bc7f09183d38234bf3be9c350765271b363cab1c Mon Sep 17 00:00:00 2001 From: AbhishekPathania Date: Wed, 18 Nov 2026 20:50:39 +0530 Subject: [PATCH 2/3] GH-1502: [JDBC] Do not close the session from getStreams endpoint handlers Endpoint handlers are built from a copy of the connection's Builder and authenticate with the connection's credentials. On a server that keys sessions by bearer token, a CloseSession from a failed endpoint fetch would end the connection's own session. ADBC's Flight SQL driver likewise never closes sessions from its endpoint clients. --- .../client/ArrowFlightSqlClientHandler.java | 10 +++- .../arrow/driver/jdbc/ResultSetTest.java | 56 +++++++++++++++++++ .../ArrowFlightSqlClientHandlerTest.java | 20 +++++++ 3 files changed, 85 insertions(+), 1 deletion(-) 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 221bbc9bd0..5338feb680 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 @@ -300,7 +300,9 @@ public void close() throws SQLException { final CallOption[] closeSessionOptions = Arrays.copyOf(baseOptions, baseOptions.length + 1); closeSessionOptions[baseOptions.length] = CLOSE_SESSION_TIMEOUT; try { - sqlClient.closeSession(new CloseSessionRequest(), closeSessionOptions); + if (builder.ownsSession) { + sqlClient.closeSession(new CloseSessionRequest(), closeSessionOptions); + } } catch (FlightRuntimeException fre) { if (catalog.isPresent()) { handleBenignCloseException( @@ -729,6 +731,11 @@ public static final class Builder { @VisibleForTesting Optional 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; @@ -776,6 +783,7 @@ public Builder() {} 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/ResultSetTest.java b/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ResultSetTest.java index 7d1bf67e45..c5c6a4fbb2 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.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 void testPartitionedFlightServerIgnoreFailure() throws Exception { } } + @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 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 47b29e1a83..dcdc418774 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 @@ -25,6 +25,7 @@ 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; @@ -190,4 +191,23 @@ private static Object[] testCloseHandlesFlightRuntimeException() { new Object[] {false, unknownError, 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(); + } } From 433267cf5687bfbfce59b12b9f97045b182ba59c Mon Sep 17 00:00:00 2001 From: AbhishekPathania Date: Thu, 19 Nov 2026 21:42:48 +0530 Subject: [PATCH 3/3] GH-1502: [JDBC] Only ignore no-session statuses from CloseSession without a catalog --- .../client/ArrowFlightSqlClientHandler.java | 17 ++++++++++--- .../ArrowFlightSqlClientHandlerTest.java | 25 ++++++++++++++++--- 2 files changed, 34 insertions(+), 8 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 5338feb680..ba923c69a9 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 @@ -27,6 +27,7 @@ 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; @@ -85,6 +86,15 @@ public final class ArrowFlightSqlClientHandler implements AutoCloseable { @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 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 options = new HashSet<>(); @@ -304,12 +314,11 @@ public void close() throws SQLException { sqlClient.closeSession(new CloseSessionRequest(), closeSessionOptions); } } catch (FlightRuntimeException fre) { - if (catalog.isPresent()) { + 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"); - } else { - // Without a catalog the driver never asked for a session; the server may not support them. - logSuppressedCloseException(fre, "closing Flight SQL session"); } } finally { try { 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 dcdc418774..12b8c5e1e5 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 @@ -151,10 +151,12 @@ public void testCloseHandlesFlightRuntimeException( verify(sqlClient).close(); } - @Test - public void testCloseSessionWithoutCatalogIsBestEffortWithTimeout() throws Exception { + @ParameterizedTest + @MethodSource + public void testCloseSessionWithoutCatalog(FlightStatusCode code, boolean shouldSuppress) + throws Exception { FlightSqlClient sqlClient = mock(FlightSqlClient.class); - doThrow(CallStatus.UNKNOWN.toRuntimeException()) + doThrow(new CallStatus(code, null, null, null).toRuntimeException()) .when(sqlClient) .closeSession(any(CloseSessionRequest.class), any(CallOption[].class)); ArrowFlightSqlClientHandler sqlClientHandler = @@ -166,13 +168,28 @@ public void testCloseSessionWithoutCatalogIsBestEffortWithTimeout() throws Excep Optional.empty(), null); - assertDoesNotThrow(sqlClientHandler::close); + 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}, + }; + } + private static Object[] testCloseHandlesFlightRuntimeException() { CallStatus benignInternalError = new CallStatus(FlightStatusCode.INTERNAL, null, "Connection closed after GOAWAY", null);