Skip to content

Add scoped connection pooling with session reset - #246

Open
quinnj wants to merge 2 commits into
mainfrom
feature/scoped-connection-pool
Open

quinnj wants to merge 2 commits into
mainfrom
feature/scoped-connection-pool

Conversation

@quinnj

@quinnj quinnj commented Sep 26, 2026 •

Copy link
Copy Markdown
Member

Applications need to keep related queries, such as an INSERT and LAST_INSERT_ID(), on one session while limiting the number of open connections. Add qualified MySQL.ConnectionPool and MySQL.with_connection for scoped, exclusive use without another dependency or new exports.

The limit includes connections being created, reset, or retired. Session cleanup drains unread responses, sends COM_RESET_CONNECTION, invalidates client statement/cursor generations, and restores the configured database, utf8mb4 and repeatable initializer. Callback failures retire the connection and preserve the original exception/backtrace; failed cleanup also retires it, and callbacks are never replayed. Closing the pool wakes waiters and rejects new leases while letting acquired leases finish. Network operations run outside the pool bookkeeping lock.

Connection ownership remains a caller contract: finish child work, statements and streaming results before returning. Owned buffered results survive. Escaped statements can still auto-reprepare; generation invalidation is not a revocable lease. With no configured database, a session that selected one is replaced because reset cannot clear that selection. Reset and initialization add network round trips; initializers must be repeatable. Unread SQL errors keep the driver's existing discard behavior.

Validation:

  • Julia 1.13: 1,960/1,960 serverless package checks; another 38/38 public pool checks each against MySQL 8.4.11 and MariaDB 11.4.13.
  • Julia 1.10: 2,360/2,360 full checks on one thread, including both live server lanes and the existing lifecycle soak.
  • Scripted peers cover bounded/exclusive leases, shutdown and creation/reset races, interrupted waits and callbacks, reset/restoration/protocol failures, old statement IDs, and buffered/streamed ownership.
  • The existing strict native workload now includes sequential pooled operations with statically known callbacks. It compiles with zero diagnostics and passes two runtime-argument executions with code generation disabled. Strict docs and the native clean-room check pass.
  • In matched local MySQL controls, pooled and equivalent manual-reset leases allocate the same bytes: 1,584/2,784 B for empty/query callbacks on 1.13 and 1,856/3,552 B on 1.10. Median latency is near the equivalent manual reset. Fresh TLS connections cost roughly 200–239 KB and 2.7–3.5 ms per callback, versus 1.6–3.6 KB and 0.8–1.1 ms pooled. These are disposable loopback measurements with ambient host load, not a network-throughput claim.

Hosted CI at 9fc59d2: all 15 checks pass, with two scheduled-only jobs skipped. The test waiter helper uses public condition notifications and passes current, minimum and nightly runtimes.

Closes #140.

Co-authored by Codex

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.

Pooled Connections

1 participant