Error explained
Database connection pool exhausted
Requests wait for a database connection and time out. Either connections are held too long, leaked, or the pool is too small for the work.
3 min read9 sectionsUpdated
On this page
| Stack | Message |
|---|---|
| Java (HikariCP) | Connection is not available, request timed out after 30000ms |
| Node (knex) | Knex: Timeout acquiring a connection. The pool is probably full. |
| Node (pg) | timeout exceeded when trying to connect |
| Python (SQLAlchemy) | QueuePool limit of size 5 overflow 10 reached, connection timed out |
| Rails | ActiveRecord::ConnectionTimeoutError: could not obtain a connection from the pool |
Causes
- Slow queries or long transactions keep connections busy; find them in traces and
pg_stat_activity. - Leaks: a code path that checks out a connection and never releases it on error.
- External calls inside a transaction: holding a connection while waiting on an HTTP API.
- Pool smaller than concurrency: e.g. Puma with 5 threads and a pool of 2.
- Many instances × pool size above the database's max_connections, so new connections are refused.
Sizing the pool
Start from the database's limit: instances × pool size must stay below max_connections with headroom for admin and migrations. With many instances, put a pooler such as PgBouncer in front. Inside each instance, the pool should match real concurrency: enough for the threads or concurrent requests that need the database at the same time, not more.
# always release, even on error
with engine.connect() as conn:
rows = conn.execute(text("SELECT ...")).all()const client = await pool.connect();
try {
await client.query('BEGIN');
// ...
await client.query('COMMIT');
} catch (e) {
await client.query('ROLLBACK');
throw e;
} finally {
client.release();
}Catching it in production
Track active, idle and pending connections per instance and alert when pending stays above zero. In traces, a long gap before the first database span is time spent waiting for a connection.
VitalRix error tracking groups exceptions recorded on OpenTelemetry spans into issues with counts, first and last seen, affected endpoints and versions, and marks an issue as regressed if it comes back after being resolved. Alert rules on error rate per service or endpoint route the problem to the right team before customers report it.
Should I just increase the pool size?
Only if the database has room and the pool is genuinely smaller than concurrency. If queries are slow, a bigger pool just moves the queue into the database.
How do I find a leak?
Enable the pool’s leak detection (for example HikariCP leakDetectionThreshold) to log the stack of connections held too long.
Related reading
- HikariCP "Connection is not available": how to fix it: Fix HikariCP Connection is not available, request timed out: use leak detection, size the pool sensibly, avoid long transactions and read pool metrics.
- ERR_CONNECTION_TIMED_OUT: why a site will not load: Why browsers show ERR_CONNECTION_TIMED_OUT, how to tell a client problem from a server one, and the firewall, DNS and server checks that find the cause.
- Error: connect ETIMEDOUT: What ETIMEDOUT means, how it differs from ECONNREFUSED, the usual causes such as firewalls, security groups and wrong IPs, and how to set timeouts.
- PHP "Allowed memory size exhausted": how to fix it: Fix PHP Fatal error: Allowed memory size exhausted. Read the error correctly, raise memory_limit for the right SAPI and stop loading big data into arrays.
- Redis connection refused: how to fix it: Fix Redis connection refused on port 6379: server stopped, bind address, protected mode and Docker networking, with redis-cli and ss commands for each cause.
- SSH connection refused: how to fix it: Fix "ssh: connect to host port 22: Connection refused": sshd stopped, wrong port, firewalls and fail2ban, and how to get back in when you are locked out.
Watch your own site and services with VitalRix
Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.