Session state and transaction affinity in a connection pool

Trace pool checkout, connection-local transaction state, session settings, and qualified commit or rollback cleanup. Transaction runtime and crash recovery · Databases.

A connection pool can hand two requests different physical sessions even when the application uses one pool object. That makes transaction affinity a runtime rule: the connection that begins a unit must carry its reads, writes, savepoint, and final commit or rollback until the unit ends.

Pool checkout and connection affinity

Read the steps as text

  1. A transaction stays on its physical connection through completion. In PostgreSQL 18 READ COMMITTED, ordinary reads exclude other uncommitted changes. R1 updates on C17; R2 reads on C18.
  2. R1 follows up on C17 within T1. The ordinary READ COMMITTED read on separate C18 sees a committed value, not R1’s uncommitted update.
  3. R1 completed T1 on C17. Its changed role and search path remain; reset is pending before another borrower can use C17.
  4. This pool applies its stated reset policy on C17 after completion. Only then does R2 check out C17 at the known baseline.
  5. The trace closes with C17 at the known baseline and available for a new checkout.

In the primary profile, request R1 checks out connection C17, begins a transaction, and updates an order. The trace uses PostgreSQL READ COMMITTED with an ordinary non-locking read, so another session does not read an uncommitted change. R1 must keep C17 for the follow-up read and COMMIT or ROLLBACK; returning only the pool object does not preserve the transaction. A second request can use C18 at the same time and should not see C17's uncommitted work under that named profile.

Before a connection returns to a pool, the owner must finish the transaction or deliberately roll it back, then apply the pool's reset policy. Reset may clear session variables, temporary objects, prepared state, warnings, or other engine-specific session settings. A successful COMMIT ends the transaction, but it does not mean every session setting has been reset. A pool that skips cleanup can leak a tenant's role, search path, isolation default, or temporary state to the next borrower. The exact reset API belongs to the driver and engine; the invariant is that the next borrower receives a known baseline.

The connection and the transaction are related but different state machines. A connection can remain open while no transaction is active. A transaction can be active while session settings remain changed. A pool release is an ownership transfer, not a transaction boundary. If a command fails and the engine marks the transaction aborted, later commands may fail until the client rolls back or uses the engine's documented recovery operation. The safe application pattern is to catch the failure, roll back the active unit on the same connection, reset session state, and release only after cleanup.

Engine profiles and savepoint recovery

Read the steps as text

  1. PostgreSQL 18 and MySQL 8.4 have named automatic-commit profiles; the driver may configure the session.
  2. Outside an explicit block PostgreSQL uses an implicit statement transaction. MySQL 8.4 defaults autocommit on; START TRANSACTION opens an explicit block.
  3. ROLLBACK TO removes work after a savepoint and keeps the outer transaction open. On C17, enrollment precedes sp_balance; two balance updates follow it, and the second fails.
  4. On C17, ROLLBACK TO sp_balance removes the later balance updates. The earlier enrollment remains uncommitted in the still-open outer transaction.
  5. Both named profiles and the savepoint recovery boundary are now explicit.

The profile compares two named engines. PostgreSQL treats a statement outside an explicit BEGIN/COMMIT block as an implicit transaction; a block groups statements and allows savepoints. MySQL 8.4 documents autocommit enabled by default. START TRANSACTION begins an explicit block and temporarily suspends automatic commit until COMMIT or ROLLBACK; that command-level suspension is distinct from changing the autocommit session variable itself. These facts explain why a driver or pool must set and reset its connection deliberately; they do not make one vendor's behavior a universal DBMS rule.

A savepoint is an inner mark, not a nested committed transaction. Suppose the connection inserts an enrollment, establishes sp_balance, then performs two balance updates. ROLLBACK TO SAVEPOINT sp_balance removes the later updates while leaving the enrollment and outer transaction active. The client can replace the failed branch and commit the surviving work. PostgreSQL documents ROLLBACK TO as the way to regain control of a transaction block put into an aborted state; MySQL's InnoDB documentation similarly keeps the transaction active after rolling back to a savepoint, with engine-specific lock details.

The boundary is operational. A checkout wrapper should record which connection began the unit, execute all statements through that handle, and make rollback idempotent enough for the driver contract. A release hook should not silently commit an abandoned unit unless that is an explicitly documented policy. The wrapper should restore session defaults even after a successful commit, because transaction completion and session hygiene solve different problems.

A useful audit record includes the connection identifier, transaction status before release, whether rollback was attempted, and the reset result. It should also distinguish a connection that was lost from one that was deliberately returned. A timeout or network break can leave the server and client with different beliefs about completion; the application must follow the driver and engine contract instead of assuming that a retry is harmless. Retrying a statement whose commit outcome is unknown may duplicate a business effect, so idempotency keys or an explicit reconciliation step belong at the application boundary.

Session variables need the same discipline. A request that changes a role, schema search path, time zone, isolation default, temporary table, or prepared statement changes the environment of the physical connection. Some drivers reset a subset automatically, while others require an explicit reset or a fresh connection. The lesson therefore asks for a stated reset policy rather than promising that pool release magically clears every kind of state.

Recap

  • Pool affinity keeps one transaction on one physical connection; a pool handle is not the transaction.
  • Commit or rollback ends the current unit, while session settings and pool reset policy govern what the next borrower inherits.
  • Autocommit, savepoints, aborted-state recovery, and implicit transaction behavior must be qualified by the named engine and driver.
Why this is a runtime boundary

The transactions lesson owns atomicity, isolation, savepoints, and the meaning of a committed unit. This lesson owns the handoff around that unit: checkout, connection affinity, failure cleanup, and session hygiene. It does not claim that every pool implements one reset command, that every engine has identical autocommit, or that DDL has one universal transaction rule.

Next, WAL, crash recovery, and the redo/undo boundary continues from the transaction handoff into durable recovery.

Revisit: Transactions and isolation for the transaction boundary this runtime handoff surrounds.

Practise the handoff with Queue Fundamentals: a queue can schedule requests, but only the checked-out connection carries one transaction's state.