Skip to content

[JDBC] Only close the server session when one may exist - #47

Open
xborder wants to merge 1 commit into
mainfrom
jdbc-close-session-on-evidence
Open

xborder wants to merge 1 commit into
mainfrom
jdbc-close-session-on-evidence

Conversation

@xborder

@xborder xborder commented Nov 30, 2026

Copy link
Copy Markdown
Owner

Summary

Flight SQL leaves the session mechanism to the server ("typically RFC 6265 cookies"), so Connection#close now sends CloseSession only when the driver has seen a sign of a session, and never fails close because of it.

 Connection#close
   ArrowFlightSqlClientHandler.close
     closeSession
-      if this handler does not own the session: return
+      if this handler does not own the session, or the tracker saw no session: return
       CloseSession (5s deadline)
-      on error: ignore UNIMPLEMENTED / INVALID_ARGUMENT / NOT_FOUND / UNAUTHENTICATED
-                when no catalog property was set, otherwise throw SQLException
+      on error: log it (warn if session options were set, else debug) and carry on
     close the gRPC channel

A new ServerSessionTracker records the two signs of a session:

ServerSessionTracker            # registered as client middleware when the handler is built
  onSessionOptionsSet()         # called by setCatalog, at connect time or via Connection#setCatalog
  onHeadersReceived(headers)    # any Set-Cookie header, including during the auth handshake
  mayHaveSession()              # either of the above

This also fixes a gap in the old check: it read only the catalog connection property, so a catalog set later with Connection#setCatalog was treated as "no session".

Evidence

ConnectionTest#testCloseOnlyClosesSessionAfterSessionOptionsAreSet connects to a server that sends no cookie:

connect; getMetaData; close        -> expect 0 CloseSession
connect; setCatalog("new_catalog"); close -> expect 1 CloseSession in total
  • Before: on main, the plain connection already sends CloseSession:
    testCloseOnlyClosesSessionAfterSessionOptionsAreSet:692 expected: <0> but was: <1>
    After: passes. ArrowFlightSqlClientHandlerTest, ConnectionTest and ResultSetTest all pass (109 tests). Handler unit tests cover: no session → no call, setCatalog → call, cookie → call, unrelated headers → no call, every failure status → close succeeds, endpoint copies never close the session.

Merge Danger

Door: two-way

Reverting restores the previous behaviour; no persisted state or API changes outside the package-private handler constructor.

Blast Radius: connection-close

  • Servers that tie a session to the bearer token and send no cookie no longer receive CloseSession; their sessions expire on the server.
  • A failed CloseSession after a catalog was set used to make close throw SQLException; it is now logged at warn.
  • Not addressed here: the builder's cookieFactory field is never registered with the client, so withRetainCookies appears to have no effect.

🤖 Generated with Claude Code

Connection#close sent CloseSession on every connection and kept a list
of status codes to ignore when no catalog was set. Flight SQL leaves the
session mechanism to the server, so the driver now sends CloseSession
only when it has seen a sign of a session:

- the connection set session options, at connect time or later through
  Connection#setCatalog, or
- the server sent a cookie, which is how sessions are typically kept.

Connections to servers without sessions no longer make an extra call on
close.

CloseSession is now best effort, as in the ADBC Flight SQL driver. A
failure is logged (at warn when the connection set session options) and
does not fail close, since the channel is closed either way and an
unclosed session expires on the server. This replaces the list of
"no session" status codes.

Previously the catalog check only looked at the catalog connection
property, so a catalog set with Connection#setCatalog after connecting
was treated as if there were no session.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant