Skip to content

GH-1502: [JDBC] Close the server session on Connection#close without a catalog - #1504

Open
AbhishekPathania wants to merge 4 commits into
apache:mainfrom
AbhishekPathania:jdbc-close-session-without-catalog
Open

AbhishekPathania wants to merge 4 commits into
apache:mainfrom
AbhishekPathania:jdbc-close-session-without-catalog

Conversation

@AbhishekPathania

@AbhishekPathania AbhishekPathania commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

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.

@github-actions

This comment has been minimized.

@AbhishekPathania
AbhishekPathania marked this pull request as draft November 18, 2026 14:15
@AbhishekPathania
AbhishekPathania marked this pull request as ready for review November 18, 2026 15:28
@lidavidm lidavidm added the enhancement PRs that add or improve features. label Nov 18, 2026
@github-actions github-actions Bot added this to the 20.0.0 milestone Nov 18, 2026
@AbhishekPathania
AbhishekPathania force-pushed the jdbc-close-session-without-catalog branch 2 times, most recently from 384b190 to 84c8253 Compare November 19, 2026 11:11
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");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This suppresses every FlightRuntimeException when no catalog is configured, including failures such as UNAVAILABLE or UNAUTHENTICATED. In those cases Connection.close() succeeds even though the server session may remain open.

Could we suppress only statuses indicating that the server does not support session catalog, and propagate other failures as we do when a catalog is configured?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, fair point. I narrowed it. Without a catalog, close now only ignores statuses that mean there's no session to close:

Everything else goes through the catalog path.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we being so prescriptive? Why can't we just let it fail? Isn't the purpose to show that something went wrong?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xborder if we let it fail, close() would throw on every server without CloseSession, which today is the default Java, Go, C++ and Rust ones. So it only skips the "no session" statuses, anything else still fails.

@xborder xborder Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AbhishekPathania, I understand what it does. What I'm still wondering is why the driver should be the one handling incomplete server implementations. I know AI makes it easy to cover edge cases like this, but conceptually I'd like to push back a bit on the decision. If a new FlightStatusCode gets added, it's unclear if they should also be part of NO_SESSION_STATUS_CODES or not

@AbhishekPathania AbhishekPathania Nov 25, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xborder I think the driver needs to handle it, since most servers don't implement CloseSession today, including the reference Java, Go, C++ and Rust ones. If close() always sends it, it has to treat "not supported" as nothing to close, otherwise close() fails on all of them. ADBC's Go driver handles it in the driver too: https://github.com/apache/arrow-adbc/blob/40323c6da8cd22079439cec704e1b9ea67e94003/go/driver/flightsql/flightsql_connection.go#L1329-L1339

On new status codes, the list only has the ones that mean there's no session to close, so a new code isn't on it and fails like any other error. Adding one later is a one-line change if a server needs it.

@AbhishekPathania
AbhishekPathania force-pushed the jdbc-close-session-without-catalog branch from 84c8253 to bc7f091 Compare November 19, 2026 15:13
@lidavidm lidavidm removed this from the 20.0.0 milestone Nov 21, 2026
@AbhishekPathania
AbhishekPathania force-pushed the jdbc-close-session-without-catalog branch from 433267c to e6c2ab8 Compare November 23, 2026 06:03
@github-actions github-actions Bot added this to the 20.1.0 milestone Nov 23, 2026
@AbhishekPathania
AbhishekPathania force-pushed the jdbc-close-session-without-catalog branch 3 times, most recently from b967096 to c580987 Compare November 24, 2026 10:08
…nt 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.
@AbhishekPathania
AbhishekPathania force-pushed the jdbc-close-session-without-catalog branch from c580987 to e1d7f15 Compare November 25, 2026 05:36
Comment on lines +308 to +330
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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

     @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");
         }
       }
     }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, moved the ownsSession check to the top of closeSession().

Comment on lines +182 to +190
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},
};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering this non-blocking as well — nice-to-have if you push another update, otherwise fine as is.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added. Flight maps DEADLINE_EXCEEDED to TIMED_OUT, so the new row is {TIMED_OUT, false}.

@AbhishekPathania

Copy link
Copy Markdown
Contributor Author

@jbonofre pushed both nits in 9a7ecdc, could you take another look when you get a chance?

sqlClient.closeSession(new CloseSessionRequest(), closeSessionOptions);
}
} catch (FlightRuntimeException fre) {
if (catalog.isEmpty() && NO_SESSION_STATUS_CODES.contains(fre.status().code())) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is catalog such a relevant piece of information here? I don't understand why close was only called when there was a catalog and I don't understand why we surpress errors if it is empty.

This seems tuned to a specific server implementation

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xborder makes sense. I kept the catalog path as it was to keep the change small, but these statuses all mean there's no session to close, catalog or not. Happy to drop the catalog check so both work the same way.

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");

@xborder xborder Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AbhishekPathania, I understand what it does. What I'm still wondering is why the driver should be the one handling incomplete server implementations. I know AI makes it easy to cover edge cases like this, but conceptually I'd like to push back a bit on the decision. If a new FlightStatusCode gets added, it's unclear if they should also be part of NO_SESSION_STATUS_CODES or not

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement PRs that add or improve features.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[JDBC] Connection#close does not close the server session unless a catalog is set

4 participants