Connections and pooling
Overview
A connection provides a communication channel and session state. A pool reuses a bounded set of connections instead of opening one per request. Borrow briefly, release in finally, roll back unfinished work, and reset session state or discard unsafe connections. Size the pool across all application instances against the server budget, leaving administrative capacity.
First example
SELECT state,COUNT(*) FROM ad_pool GROUP BY state ORDER BY state;Observed SQLite result (rows in JSON):
[ [ "idle", 2 ], [ "leased", 1 ]]How it works
The SQLite table models three pool slots, not actual server sessions. An idle slot is available; a leased slot belongs to a request. A real pool must handle wait deadlines, cancellation, broken connections, authentication and session reset. Transaction pooling may restrict session-specific features. A pool is not unlimited concurrency.
Practical example
BEGIN;UPDATE ad_pool SET state='leased' WHERE id=2 AND state='idle';SELECT COUNT(*) FROM ad_pool WHERE state='idle';ROLLBACK;Observed SQLite result (rows in JSON):
[ [ 1 ]]Practice and mistakes
Practice: count the available slots after leasing slot 2 in a transaction. Hint: filter state; then roll back. Check: one idle slot during the transaction and two afterward. Mistake: returning a connection with an open transaction.
PostgreSQL 17 example — NOT RUN
SELECT name,setting,unit FROM pg_settings WHERE name IN ('max_connections','superuser_reserved_connections','reserved_connections') ORDER BY name;Expected result, not executed: "Connection limits vary by server; not pool occupancy."