DBI pool connection timeout and server-side session alignment
0 reputation · 16 Nov 2021, 13:33 UTC
0 reputation · 16 Nov 2021, 13:33 UTC
The pool package manages a cache of database connections to reduce handshake overhead, utilizing a validation mechanism and an idle timeout to reclaim resources. When integrating this with database servers that enforce their own strict idle-session timeouts, a discrepancy between the client-side pool timer and the server-side session limit can occur.
If the server terminates a session before the pool package identifies it as idle or performs a validation check, the application may encounter stale connection errors during checkout.
pool idle timeout is synchronized with server-side session limits?DBI core does not implement connection pooling. Pooling is provided by external modules such as DBI::Pool, DBIx::Connector, DBIx::Pool and driver-specific pools. Timeout and validation behavior is therefore module-specific, not DBI itself.
Confirmed fact: server-side session state is bound to the physical database connection. A handle returned from a pool can carry stale state from a previous checkout, including SET ROLE, search_path, time zone, temporary tables, prepared statements and transaction state.
Align pool lifetimes to be strictly shorter than server limits and re-apply session state on checkout.
Validation does not trigger automatically on every checkout in all modules. It is module-specific and usually opt-in.
Stale connection errors during checkout commonly occur when the DB server closes idle connections via wait_timeout, idle_in_transaction_session_timeout or equivalent, while the pool still marks them alive. If pool idle timeout is longer than the server timeout, or validation is interval-based rather than checkout-based, the application receives a dead handle.
One missing diagnostic detail that changes the recommendation: which pooling module and DB server/version you are using, and whether session alignment is for security context such as SET ROLE versus non-security settings like time zone. The parameter names and safe defaults differ per module.
Use comments to ask for clarification. Post a solution as an answer.
6,260 reputation · 16 Nov 2021, 19:56 UTC
The validationInterval setting only sweeps connections that have been idle for at least that many seconds; it does not run on every checkout and does not reset when a connection is taken from the pool.
validationInterval to a value safely below the server’s idle timeout (e.g., ≤ 0.8 × server timeout) so idle connections are tested and dropped before the server closes them.validate_on_checkout (or an equivalent checkout callback) in addition to, or instead of, the periodic sweep.