Skip to content

Reconnect to the database the session last used - #266

Open
quinnj-bot[bot] wants to merge 2 commits into
mainfrom
claude/reconnect-current-database
Open

quinnj-bot[bot] wants to merge 2 commits into
mainfrom
claude/reconnect-current-database

Conversation

@quinnj-bot

@quinnj-bot quinnj-bot Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Why

With reconnect=true, a connection that changed its database with USE silently goes back to
the database it was opened with after a reconnect. Every unqualified query after the reconnect
then reads and writes the wrong tables, with no error. MySQL.jl 1.x (libmariadb) and
libmysqlclient reconnect to the database the session last used.

Repro against MySQL 8.4, MariaDB 11.4 and MySQL 8.0 (2.4.0):

conn = DBInterface.connect(MySQL.Connection, host, user, pw; db="app", reconnect=true)
DBInterface.execute(conn, "USE reporting")
# the server drops the connection (KILL CONNECTION, wait_timeout, network blip)
DBInterface.execute(conn, "SELECT 1")          # 2013, as documented
DBInterface.execute(conn, "SELECT DATABASE()") # 2.4.0: "app"; 1.5.1: "reporting"

What changed

The server already reports every change of the default database (USE, or dropping the
current database) in a SESSION_TRACK_SCHEMA block of its OK packet. The driver parsed that
block (Protocol.schema_change) but never used it.

  • Protocol.Session keeps schema: the database requested at authentication, then each
    SESSION_TRACK_SCHEMA change seen in an OK packet (command OK, result-set terminator, auth OK).
  • ensure_live! reconnects with the dead session's schema. connect(opts; db) takes the
    database to open and sets CLIENT_CONNECT_WITH_DB to match, so a connection opened without
    a database can come back into the one a USE selected, and one whose database was dropped
    comes back with none.
  • One line in the reconnect row of the options table.

Verification

  • New serverless test reconnect restores the database the session last reported (fake peer
    that records the database in each handshake). On main it fails: the server sees
    ["<none>", "<none>", "<none>"] instead of ["<none>", "other", "<none>"]. On this branch it passes.
  • Live, with a local script (not part of this PR) on MySQL 8.4.13, MariaDB 11.4.13 and MySQL 8.0.36, opening
    with and without a database: main reconnects into the original database; this branch
    reconnects into the USEd one, and after DROP DATABASE of the current one into none.
  • Local Pkg.test() on Julia 1.12.6, serverless lanes plus the --trim=safe workload
    (MYSQL_INTEGRATION=0 MYSQL_PERF_GATES=0): MySQL | 1917 1917, trim compile clean. The
    same suite on main with only the new test: 1909 pass, 1 fail (the new test). The Docker
    integration lanes run in CI.

Reviewed by re-reading the diff for: every OK-packet parse site (command OK, result
terminator, auth OK; EOF terminators carry no session state); a failed reconnect keeps the
dead handle, so the retry uses the same database; init_command and a USE inside
multi-statement or CALL results go through the same OK parsing; session tracking off
(session_track_schema=OFF) leaves the original database, as in libmysqlclient.

🤖 Generated with Claude Code

quinnj and others added 2 commits November 19, 2026 03:17
The fake peer records the database of each handshake: after USE, a
dropped connection must come back into that database, and after the
current database is dropped, into none.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The server reports USE (and dropping the current database) in a
SESSION_TRACK_SCHEMA block. The driver parsed the block but reconnected
with the database from connect time, so after a reconnect unqualified
queries ran against a different database. libmysqlclient and 1.x follow
the tracked schema.

Protocol.Session keeps the schema (requested at authentication, then
each tracked change in an OK packet), and ensure_live! reconnects with
it; connect(opts; db) sets CLIENT_CONNECT_WITH_DB to match.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

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