Fix HikariPool Connection Is Not Available Request Timed Out
Why HikariCP reports that a connection is not available, how to tell a leak from a pool that is too small, and how to size the pool for the database.

Under load, requests start failing after exactly 30 seconds with this line:
java.sql.SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 30000ms (total=10, active=10, idle=0, waiting=47)The message is a snapshot of the pool at the moment of failure, and it says everything: ten connections exist, all ten are active, none idle, and 47 threads are waiting for one. HikariCP gave each waiting thread 30 seconds (connectionTimeout, default 30,000 ms) and then gave up. The instinct is to raise maximumPoolSize. That is usually the wrong first move. A pool that is full is a symptom, and the cause is that connections are held too long, taken too often or the database is already as busy as it can be. This post shows how to find who holds the connections, the usual holders, how to size the pool with a formula, what to check on the database side and what to do when the pool really is too small. Examples use HikariCP 6, Spring Boot 4 and PostgreSQL 17.
Do not start by raising the pool size
A bigger pool sends more concurrent queries to a database that is probably the actual bottleneck. If the database was already saturated, more connections make each query slower, which holds each connection longer, which makes the pool even more starved. The queue moves from the pool to the database and the latency gets worse. The HikariCP authors' pool-sizing note, About Pool Sizing, makes the counterintuitive point that a small pool often outperforms a large one. Read the message first, then decide:
| Pool snapshot | Meaning | Direction |
|---|---|---|
active=10, idle=0, waiting=47 | Connections are all busy; many wait | Find why they are held long |
active=2, idle=8, waiting=0 plus a timeout | Pool is fine; the failure is elsewhere | Network or database reachability |
total=0 | The pool cannot create any connection | Database down or credentials, see connection refused |
Only the first row is a capacity problem, and it has two sub-causes: connections held too long, or too many requests for the connections available.
Find who holds the connections
Turn on HikariCP's leak detection, which logs a stack trace for any connection held longer than a threshold:
spring:
datasource:
hikari:
maximum-pool-size: 10
connection-timeout: 5000
leak-detection-threshold: 10000 # log if held more than 10 secondsWARN com.zaxxer.hikari.pool.ProxyLeakTask - Connection leak detection triggered for org.postgresql.jdbc.PgConnection@3f2a1c on thread http-nio-8080-exec-14, stack trace follows
java.lang.Exception: Apparent connection leak detected
at dev.javahacks.orders.ReportService.monthlyTotals(ReportService.java:48)
at dev.javahacks.orders.ReportController.totals(ReportController.java:27)The stack names the method that took the connection and has not returned it. Note that this log means "held longer than 10 s", not necessarily "leaked": a slow report legitimately holds a connection. Whether it ever returns is visible by whether a later line says Previously reported leaked connection ... was returned to the pool. Set the threshold above your slowest normal query and below the connection timeout so the log precedes the failures.
A second view is the database's. See what each connection is doing right now:
SELECT pid, state, wait_event_type, now() - xact_start AS xact_age, left(query, 60) AS query
FROM pg_stat_activity
WHERE datname = 'shop'
ORDER BY xact_start; pid | state | wait_event_type | xact_age | query
-------+---------------------+-----------------+----------+---------------------------
20114 | idle in transaction | Client | 00:04:12 | select * from orders where id = $1
20121 | active | IO | 00:00:38 | select o.* from orders o join ...idle in transaction with a large age is the classic: the application started a transaction, ran a query and then did something else (a remote call, a sleep, an unrelated loop) while keeping the connection checked out. Everything in that state is a candidate. Thread dumps of the application, taken twice 10 seconds apart, show which threads wait inside HikariPool.getConnection and which hold a connection and are blocked elsewhere; the reading technique is in finding blocked threads in a dump.
The usual holders
A transaction wrapped around slow non-database work. The most common:
@Transactional
public Report build(long id) {
Order order = orders.findById(id).orElseThrow(); // takes a connection
String enriched = rateClient.fetch(order.currency()); // 3 s HTTP call, connection held
return report(order, enriched);
}The transaction holds a connection for the entire method, including the HTTP call. Move the remote call outside the transactional boundary, or load what you need, close the transaction and then call.
A request that fetches in a loop (N+1). One request issuing 200 small queries holds a connection for the duration, and the sum of those queries is the hold time. The pattern and its fixes are in the N+1 problem and how to fix it.
Open session in view. With spring.jpa.open-in-view=true (the default), the persistence context, and its connection once first used, stays open until the response is written, including slow serialization and a slow client. Set it to false and load what each endpoint needs inside the service.
A slow or missing query plan. A query that takes 8 seconds instead of 8 milliseconds holds its connection a thousand times longer. Find it with the database's slow query log and the plan, per reading an EXPLAIN plan.
A real leak. Code that obtains a connection and does not close it on every path, such as an early return before close(). The fix is try-with-resources, always. Spring's JdbcTemplate, JdbcClient and the repositories close for you; the leaks come from hand-written dataSource.getConnection() calls, described in JDBC connection leak debugging.
Nested connection use. A method holding a connection calls another that asks for a second one from the same pool. With a pool of 10 and 10 threads each holding one and waiting for another, all threads wait for each other: a deadlock in the pool, which no timeout of the database can see. Avoid taking a second connection while holding one, or use REQUIRED propagation so the inner call joins the outer transaction.
Size the pool with a formula
After the holders are fixed, size what remains. The baseline from the HikariCP wiki, itself from PostgreSQL's guidance:
connections = (core_count x 2) + effective_spindle_countFor a database server with 8 cores and SSD storage (effective spindles near 1), that is 17 connections for the whole database, shared by every application instance. The consequence is the important part: the pool size per instance is the database's budget divided by the number of instances. With 17 connections available and 4 application instances, each gets a pool of 4. Raising each to 20 means 80 connections competing for the same 8 cores, which is slower than 17. Check how many are open against the limit:
SHOW max_connections;
SELECT count(*) FROM pg_stat_activity; max_connections
-----------------
100
(1 row)
count
-------
43Here 43 of 100 are in use, and an autoscaler that adds instances can exhaust the rest. Cap the instance count or put a pooler (PgBouncer) in front. A throughput check from the other side uses Little's law: the connections you need equal queries per second times seconds per query held. A service doing 200 requests per second, each holding a connection for 20 ms, needs 200 x 0.02 = 4 connections busy on average, so a pool of 10 has headroom. If the same service holds each for 400 ms, it needs 80, and the answer is not a pool of 80 but making the requests hold less. The fuller treatment with two worked examples is in the HikariCP pool sizing formula.
Check the database side
If the application looks right and the pool is still starved, the database is the bottleneck. Confirm that it is working hard and not waiting on locks:
SELECT wait_event_type, wait_event, count(*)
FROM pg_stat_activity
WHERE state = 'active'
GROUP BY 1, 2
ORDER BY 3 DESC; wait_event_type | wait_event | count
-----------------+---------------+-------
Lock | transactionid | 7
IO | DataFileRead | 3Seven sessions waiting on transactionid are blocked by another transaction's row locks: one long-running update has stalled the others, and the pool filled behind it. Find the blocker (pg_blocking_pids(pid)), fix the long transaction and consider lock timeouts (SET lock_timeout) so a wait fails fast. High DataFileRead waits mean the working set does not fit in memory; look at indexes and the cache. CPU saturation on the database host means the queries are the problem, not the pool. Also compare the database's connection count with max_connections to confirm you are not hitting its limit. The pool has useful metrics of its own (hikaricp.connections.active, hikaricp.connections.pending, hikaricp.connections.timeout) through Micrometer, and a chart of pending rising before the timeouts start is the earliest warning you can get.
Where this does not apply
If the message is Failed to obtain JDBC Connection at startup, the pool never filled, and the cause is reachability or credentials, as in connecting to a database step by step. If the pool is mostly idle and requests still time out, the stall is in something else, a lock, a slow dependency or garbage collection. And if the workload really is a burst of independent short queries that exceed the database's capacity, the answer is a queue in front of it or more database capacity; more connections cannot create throughput the database does not have. The reference for every setting above is the HikariCP configuration table, and the end-to-end project setup is in Spring Boot from first project to production.


