Quick revision
The short answer
The 180 request threads are parked inside HikariPool.getConnection, which means they are waiting for a free database connection: the pool is exhausted. Either the connections are held too long, by slow queries, remote calls inside transactions or leaks, or the pool is too small for the load. Next we look at the threads that do hold connections, at Hikari's active, idle and pending metrics and at the database's slow queries; short-term we keep a sensible connection timeout and shed load, and long-term we shorten transactions and fix the slow queries.
What to remember
In this answer, we read the stack as request threads parked in HikariPool.getConnection waiting for a connection, recognised pool exhaustion, and turned to the threads holding connections: slow queries, remote calls inside transactions, or leaks with no holder at all. We confirmed with Hikari's active, idle, pending and usage metrics and the database's view of its sessions, eased the incident with a short connection timeout and load shedding, and fixed it for good by shortening transactions, fixing slow queries and closing connections properly before touching the pool size.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The 180 request threads are parked inside HikariPool.getConnection, which means they are waiting for a free database connection: the pool is exhausted. Either the connections are held too long, by slow queries, remote calls inside transactions or leaks, or the pool is too small for the load. Next we look at the threads that do hold connections, at Hikari's active, idle and pending metrics and at the database's slow queries; short-term we keep a sensible connection timeout and shed load, and long-term we shorten transactions and fix the slow queries.
Why this question is asked
Connection pool exhaustion is one of the most common causes of latency incidents in Spring services, and a thread dump identifies it within seconds if we know the shape. It is also a trap for quick fixes: doubling the pool often makes things worse by putting more load on a database that is already the bottleneck. Interviewers ask this to see whether we can read the stack, and whether our next steps go to the cause instead of the symptom. In this answer, we will recap how a pool hands out connections, decode the stack in the question, find the threads that hold the connections, and then cover the metrics, the short-term relief and the lasting fixes.
1A Quick Recap of a Connection Pool
Opening a database connection takes a TCP handshake, authentication and session setup, often 20 to 50 ms, so a service keeps a pool of open connections and lends them out. HikariCP, the default pool in Spring Boot, has a fixed maximum, maximumPoolSize, which is 10 by default and is often set to 20 or 50. A thread borrows a connection, uses it for one transaction or query, and gives it back when it closes it.
When every connection is lent out, a new borrower waits. Hikari waits at most connectionTimeout, 30 seconds by default, and then throws SQLTransientConnectionException with a message like 'Connection is not available, request timed out after 30000ms'. Until then the thread just sits there, holding its request, its memory and its place in the Tomcat pool.
2Decoding the Stack in the Question
Reading from the bottom of the stack up, the request thread called into our repository, the repository asked Spring for a connection, Spring asked HikariPool.getConnection, and Hikari, finding no idle connection, waited for one to be handed back. Internally Hikari parks the waiting thread on a SynchronousQueue, a queue with no capacity where a returning connection is handed directly to a waiting thread. That is the poll in the stack, and Unsafe.park is how a thread waits in java.util.concurrent.
The dump in the question says WAITING (parking); because Hikari waits with a timeout, most JDKs print TIMED_WAITING (parking) here, and either way the thread is parked. Notice that the waiting threads tell us almost nothing about the cause. They are the victims. 180 out of 200 Tomcat threads waiting means the service can only work on about 20 requests at a time, and with a pool of, say, 20 connections, that is exactly what we would expect. The question is what the holders are doing.
3Finding the Threads That Hold Connections
The remaining 20 threads in the dump are the interesting ones. We look for request or worker threads that are not in getConnection and whose stacks contain JDBC or our transactional code. Their top frames usually fall into one of three patterns.
The first pattern is a slow query: the thread is RUNNABLE in a socket read inside the database driver, such as org.postgresql.core.PGStream.receive. The database is slow to answer, so each connection is held for seconds. The second is a remote call inside a transaction: the stack shows an HTTP client, such as RestClient or WebClient.block(), with a @Transactional method of ours lower down. The connection was taken when the transaction began and stays idle while we wait on another service. The third is a leak: connections borrowed and never closed, for example from a DataSource used directly without try-with-resources. Leaked connections have no thread at all in the dump, so if the holders do not add up to the pool size, we suspect a leak.
6 messages in order. Play them, or step through them one at a time.
For 2 seconds the connection does no database work, but no other thread can use it.
4Checking the Metrics and the Database
The dump is a snapshot, so we confirm with metrics over time. Spring Boot with Micrometer publishes Hikari metrics such as hikaricp.connections.active, hikaricp.connections.idle, hikaricp.connections.pending (threads waiting), hikaricp.connections.acquire (time to get a connection) and hikaricp.connections.usage (how long each connection is held). Active pinned at the maximum, idle at zero and pending in the hundreds confirm exhaustion; usage tells us whether connections are held for 5 ms or 2,000 ms, which separates 'too small' from 'held too long'.
On the database side, we look at what those connections are running. In PostgreSQL, pg_stat_activity shows each session's current query, its state and how long it has run; many sessions 'idle in transaction' point at the remote-call pattern, and long-running active queries point at slow SQL, which pg_stat_statements can rank. Hikari's leakDetectionThreshold setting logs a stack trace for any connection held longer than the threshold, which finds leaks and long holders directly.
| Observation | Likely cause | Next step |
|---|---|---|
| Holders in driver socket reads | Slow queries | pg_stat_statements, EXPLAIN, indexes |
Holders in HTTP clients under @Transactional | Remote calls inside transactions | Move the call out of the transaction |
| Holders fewer than pool size | Connection leak | leakDetectionThreshold, try-with-resources |
| Short usage, high pending, healthy database | Pool too small for the load | Raise the pool size carefully |
5Short-Term Relief and Lasting Fixes
During the incident, we want requests to fail fast rather than queue for 30 seconds. Lowering connectionTimeout to a few seconds, below the client's own timeout, turns hanging requests into quick errors that a load balancer or a retry can handle. Shedding load helps as well: a rate limit or a concurrency limit at the edge, or disabling an expensive endpoint, keeps the pool for the important traffic. If a single slow query is the cause, killing it or rolling back the deployment that introduced it gives immediate relief.
The lasting fixes go to the holding time. We move remote calls out of transactions so a connection is only held while we actually talk to the database, fix or index the slow queries, and close every connection in try-with-resources. Only then do we look at the size. A bigger pool helps when connections are held briefly and the database has spare capacity; if the database itself is the bottleneck, more connections just mean more queries competing for the same CPU and disks. Watch out for one more trap: code that takes a second connection while holding the first, for example a REQUIRES_NEW transaction called from inside another transaction, can deadlock the pool when every thread holds one and waits for another.
6Follow-up questions
Why not simply set maximumPoolSize to 200 to match the Tomcat threads?
Because the database has to run those 200 connections. Each active connection uses memory and CPU on the database server, and beyond a point more concurrent queries make each one slower. If the real problem is a 2-second remote call inside a transaction, 200 connections just let 200 idle transactions sit open. Pool size should follow what the database can serve, and the holding time should be fixed first.
Does open session in view affect this?
It can. Spring Boot enables spring.jpa.open-in-view by default, which keeps the persistence context open for the whole web request so lazy loading works in the view. Depending on the configuration, the JDBC connection can stay associated with the request longer than the transaction, which stretches holding time. Many teams turn it off; check your configuration and measure hikaricp.connections.usage before and after.
Do virtual threads fix pool exhaustion?
No. They remove the limit on request threads, so instead of 180 Tomcat threads waiting we might see 20,000 virtual threads waiting in the same getConnection call. The pool, and the database behind it, remain the limit, so the same fixes apply.
7Conclusion
In this answer, we read the stack as request threads parked in HikariPool.getConnection waiting for a connection, recognised pool exhaustion, and turned to the threads holding connections: slow queries, remote calls inside transactions, or leaks with no holder at all. We confirmed with Hikari's active, idle, pending and usage metrics and the database's view of its sessions, eased the incident with a short connection timeout and load shedding, and fixed it for good by shortening transactions, fixing slow queries and closing connections properly before touching the pool size.
Open with
Those threads are request threads waiting for a database connection. HikariPool.getConnection found no idle connection and parked the thread on a SynchronousQueue until one is returned or connectionTimeout, 30 seconds by default, runs out.
Then, if they want more
So the pool is exhausted, and these 180 threads are victims, not the cause. Next I would look at the other threads in the dump, the ones holding connections. If they are in the JDBC driver reading from a socket, we have slow queries.
If they are in an HTTP client below a @Transactional method, we are holding connections across remote calls. If fewer threads hold connections than the pool size, something is leaking connections, which leakDetectionThreshold can confirm with stack traces.
I would check the Hikari metrics: active at the maximum, idle at zero and high pending confirm exhaustion, and connection usage time tells me whether connections are held for milliseconds or seconds. On the database I would look at pg_stat_activity for long queries or sessions idle in transaction.
To get through the incident I would lower the connection timeout so requests fail fast and shed load at the edge.
The lasting fix is to shorten the time each connection is held, by moving remote calls out of transactions and fixing slow queries, and only then to resize the pool, since a bigger pool just pushes more load onto a database that may already be the bottleneck.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- java.util.concurrent, package documentation, JLS chapter 17, Threads and Locks
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "During a latency incident a thread dump shows 180 of 200 Tomcat threads like this...": https://scrivio-interview-library.netlify.app/java/concurrency-problems/5c7c620e6f/