Quick revision
The short answer
Each request holds one connection for its outer transaction and then asks the pool for a second one, because REQUIRES_NEW suspends the outer transaction and starts a new one on a new connection. With 10 concurrent requests, all 10 connections are held by outer transactions and every thread waits for an eleventh that never comes, until connectionTimeout fails them. We fix it by removing the nested transaction or moving it out of the outer one, or by making sure the pool covers the nested demand: HikariCP's formula is pool size = threads x (connections per thread - 1) + 1.
What to remember
In this answer, we saw that REQUIRES_NEW suspends the outer transaction and starts a new one on a second connection, so a thread holds two connections at once. When as many requests arrive as the pool has connections, every thread holds one and waits for another, and the service freezes until connectionTimeout fails them. HikariCP's formula, threads x (connections per thread - 1) + 1, gives the safe minimum. The best fix is to remove the nested transaction or run it after the outer one returns; otherwise we limit concurrency below what the pool can serve, and keep a short connectionTimeout as a safety net, not as the cure.
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.
Each request holds one connection for its outer transaction and then asks the pool for a second one, because REQUIRES_NEW suspends the outer transaction and starts a new one on a new connection. With 10 concurrent requests, all 10 connections are held by outer transactions and every thread waits for an eleventh that never comes, until connectionTimeout fails them. We fix it by removing the nested transaction or moving it out of the outer one, or by making sure the pool covers the nested demand: HikariCP's formula is pool size = threads x (connections per thread - 1) + 1.
Why this question is asked
This is a pool deadlock, and it is nasty because it only appears under load. In development, with one or two requests at a time, every request finds its second connection and the code works perfectly. Under a burst of traffic, the whole service freezes at once. Interviewers like this question because it connects Spring's transaction propagation with how a connection pool behaves, and the fix requires a design decision, not only a bigger number. In this answer, we will recap what REQUIRES_NEW does with connections, reproduce the deadlock, work out the pool size HikariCP's documentation gives for this case, and then go through the fixes from best to last resort.
1A Quick Recap of REQUIRES_NEW
A Spring transaction is tied to one database connection. When a method annotated with @Transactional starts, the transaction manager borrows a connection from the pool, turns off auto-commit and binds the connection to the current thread. Every repository call on that thread uses the same connection until the transaction commits or rolls back, and only then is the connection returned.
Propagation decides what happens when a transactional method calls another one. The default, REQUIRED, simply joins the existing transaction and uses the same connection. REQUIRES_NEW is different: it suspends the outer transaction and starts a completely independent one, which commits or rolls back on its own. Independent means a separate connection. The outer connection is not returned while it is suspended, because the outer transaction is still open and will continue afterwards. So for a moment, one thread holds two connections.
2Reproducing the Deadlock
A typical case is an audit record that must be saved even if the main work fails.
6 messages in order. Play them, or step through them one at a time.
Each thread holds what the others need.
Notice that no thread is doing anything wrong on its own. Each one needs two connections for a short moment. With 10 threads arriving together, each one gets its first connection, the pool is now empty, and every thread asks for a second. None of them can finish, so none of them returns its first connection.
3The Pool Size HikariCP's Documentation Gives
HikariCP's pool sizing page covers exactly this situation. To be sure that a deadlock cannot happen, the pool needs pool size = Tn x (Cm - 1) + 1, where Tn is the maximum number of threads that use the pool at the same time and Cm is the maximum number of connections a single thread holds at once.
The reasoning is that if every thread holds one connection fewer than its maximum, Tn x (Cm - 1) connections are in use, and one extra connection lets at least one thread complete, release both, and unblock the rest. For our service, Cm is 2. With 10 concurrent threads, the minimum is 10 x (2 - 1) + 1 = 11. With a Tomcat default of 200 request threads, it would be 201, which shows why sizing the pool for this pattern is the wrong direction.
4Fix 1: Remove the Nested Transaction or Move It Out
The best fix is to ask whether the audit record really needs its own transaction. If it is fine for the audit row to commit or roll back with the order, plain REQUIRED is enough: it reuses connection 1 and the problem disappears.
If the audit must survive a failed order, we can run it after the outer transaction has finished instead of inside it. The caller, for example the controller or a small facade that is not transactional, calls placeOrder() first and then record(), so each call borrows and returns its own connection in turn, and a thread never holds two.
But there is a catch with one tempting alternative. A TransactionSynchronization afterCommit() callback, or a @TransactionalEventListener in the AFTER_COMMIT phase, runs after the commit but before the outer connection is released, and Spring requires REQUIRES_NEW for any transactional work there. So that approach still holds two connections at once. Calling the second step after the outer method has returned is what really frees the first connection. An asynchronous option, such as publishing an event to a queue, also works when the audit can be a little later.
5Fix 2: Limit Concurrency or Size the Pool for the Nested Demand
If the nested transaction must stay, we make the numbers safe. Either we limit the number of threads that can enter this code path at once to below what the pool can serve, or we size the pool from the formula. With a pool of 10 and two connections per thread, at most 9 threads may be inside placeOrder() at the same time: 9 x 1 + 1 = 10.
Sizing the pool to 11 or more also works when concurrency is truly bounded, but the database must then handle the extra connections across every instance.
6Why connectionTimeout Is Not the Fix
HikariCP's connectionTimeout eventually breaks the deadlock: after 30 seconds by default, waiting threads get an SQLTransientConnectionException, their outer transactions roll back, and connections return to the pool. Lowering it to 2 or 3 seconds makes the service fail and recover sooner.
But it only turns a freeze into a wave of errors that returns with every burst. The root cause is the design, one thread needing two connections, so we fix the design and treat the timeout as the safety net.
7Follow-up questions
How can we find code that needs two connections per thread?
Search for REQUIRES_NEW and for code that uses two DataSources in one request, and then test under concurrency equal to the pool size. A load test with exactly pool-size concurrent requests reproduces this deadlock in seconds, while normal tests never do. HikariCP's pending metric jumping to the number of threads while active stays at the pool size is the signature.
Does NESTED propagation have the same problem?
No. NESTED uses a savepoint inside the same transaction and the same connection, so it never needs a second connection. It lets the inner part roll back to the savepoint without undoing the outer work, but it cannot commit independently: if the outer transaction rolls back, the nested work rolls back too. It works with DataSourceTransactionManager on JDBC; support with JPA depends on the transaction manager and settings, so check your version.
8Conclusion
In this answer, we saw that REQUIRES_NEW suspends the outer transaction and starts a new one on a second connection, so a thread holds two connections at once. When as many requests arrive as the pool has connections, every thread holds one and waits for another, and the service freezes until connectionTimeout fails them. HikariCP's formula, threads x (connections per thread - 1) + 1, gives the safe minimum. The best fix is to remove the nested transaction or run it after the outer one returns; otherwise we limit concurrency below what the pool can serve, and keep a short connectionTimeout as a safety net, not as the cure.
Open with
This is a pool deadlock caused by REQUIRES_NEW. Each request starts a transaction, which borrows a connection and keeps it until commit. REQUIRES_NEW suspends that transaction and starts an independent one, which needs a second connection while the first is still held.
Then, if they want more
With a pool of 10 and 10 concurrent requests, all 10 connections are held by outer transactions and every thread waits for a second, so nobody finishes and nobody releases anything, until connectionTimeout throws after 30 seconds. HikariCP's documentation gives the safe minimum as threads times connections per thread minus one, plus one, so 10 threads need 11 connections.
My first fix would be to question the nested transaction: if the inner work can join the outer one, REQUIRED solves it. If it must commit independently, like an audit record, I would call it after the outer transactional method returns, from a non-transactional facade, so each step borrows one connection at a time.
An afterCommit callback does not help because the outer connection is still held there. If neither is possible, I would bound concurrency with a semaphore or bulkhead so at most 9 threads enter that path, and lower connectionTimeout so failures are fast, but the real fix is the design.
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.sql, package documentation
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 "A service deadlocks under load: all threads wait for connections, and none are released....": https://scrivio-interview-library.netlify.app/databases/jdbc/7f019c3739/