Scrivio Interview library
ScenarioIntermediate7 min read · updated 12 November 2026

Each request in a service opens a transaction with one connection and then calls a helper that requests a second connection from the same Hikari pool of size 10. Under load, everything hangs until connection timeouts. Explain and fix.

When 10 requests each hold one of the pool's 10 connections and then ask for a second, no connection is free and none will ever be released, because every holder is waiting for another one. This is a resource deadlock on the pool rather than on locks, so the thread dump shows threads waiting in HikariPool.getConnection until connectionTimeout fires. We fix it by reusing the same connection or transaction in the helper, or by restructuring so a request holds only one connection at a time; we catch it in tests by running with a very small pool, and we watch for REQUIRES_NEW propagation in Spring, which opens a second connection.

Background to this question

Pool deadlocks are sneaky because the code looks harmless: a service method opens a transaction, and a helper somewhere below quietly gets its own connection. With light traffic there are always spare connections, so nothing goes wrong. Under load, the service freezes completely and only recovers when timeouts fire, which looks like a database outage although the database is idle. In this answer, we will recap how the pool hands out connections, walk through why the second request causes a deadlock, read the symptoms, and then go through the fixes, the test that catches it, and the Spring feature that causes it most often.

1A Quick Recap of a Connection Pool

HikariCP keeps a fixed number of open database connections, here 10, and lends them out. A thread that calls getConnection() receives a free connection or waits until one is returned. If it waits longer than connectionTimeout, 30 seconds by default, it gets an SQLTransientConnectionException. A connection goes back to the pool only when the code using it closes it, which for a Spring transaction happens when the transaction ends.

In a Spring @Transactional method, the transaction manager takes one connection at the start and binds it to the current thread. Repository calls inside the transaction reuse that bound connection. But code that bypasses the transaction, for example by calling dataSource.getConnection() directly, or starts a new transaction, gets a different connection from the pool.

2Walking Through the Deadlock

Here is the pattern from the question. The service method holds a connection for its transaction, and the helper asks the pool for another one.

Java
@Transactional
public void placeOrder(Order o) {
orders.save(o); // uses connection #1, bound to this transaction
auditHelper.record(o); // asks the pool for connection #2
}
// AuditHelper
public void record(Order o) {
try (Connection c = dataSource.getConnection()) { // a second, separate connection
// insert into audit ...
}
}
// 1 request: holds 1, gets 1 more -> 2 of 10 used, fine
// 10 requests: each holds 1 (10 of 10 used), each waits for a second -> nobody can continue
Step throughTen requests and a pool of ten
Requests 1-10Hikari poolDatabase
getConnection() x10 for transactions
10 connections lent; pool empty
INSERT orders on each connection
getConnection() x10 for the helper
no free connection; all wait
after 30 s: SQLTransientConnectionException
rollback; connections returned

7 messages in order. Play them, or step through them one at a time.

The database is idle throughout; the waiting happens entirely inside the pool.

Suppose 10 requests arrive together. Each one starts its transaction and takes a connection, so the pool is empty. Then each calls the helper and waits for a second connection. The only way a connection returns to the pool is for a transaction to finish, and no transaction can finish without its second connection. Every thread holds one and waits for one: hold and wait, with a cycle through the pool. After 30 seconds the waits time out, the transactions roll back and release their connections, and the next burst of requests does the same thing.

3What the Symptoms Look Like

A thread dump shows the request threads not blocked on any monitor but waiting inside the pool, typically TIMED_WAITING with HikariPool.getConnection near the top of the stack and our helper just below it. There is no Found one Java-level deadlock line, because the pool's connections are not locks that the JVM tracks. Hikari's metrics show active connections at the maximum, idle at zero and pending growing, while the database reports almost no active queries. Then the errors arrive in a wave every connectionTimeout.

Text
"http-nio-8080-exec-7" ... TIMED_WAITING (parking)
at java.util.concurrent.SynchronousQueue.poll(...)
at com.zaxxer.hikari.util.ConcurrentBag.borrow(...)
at com.zaxxer.hikari.pool.HikariPool.getConnection(...)
at com.acme.audit.AuditHelper.record(AuditHelper.java:31)
at com.acme.orders.OrderService.placeOrder(OrderService.java:18)
HikariPool-1 - Connection is not available, request timed out after 30000ms
(total=10, active=10, idle=0, waiting=24)

Notice that the stack names exactly the code that asks for the second connection, so the dump points straight at the fix. The internal frames under getConnection differ between Hikari versions.

4Fix 1: Reuse the Connection or Transaction

The cleanest fix is for the helper to take part in the caller's transaction instead of getting its own connection. If it uses a Spring repository, JdbcTemplate or JdbcClient, it automatically uses the connection bound to the current transaction.

Java
@Component
class AuditHelper {
private final JdbcClient jdbc; // uses the transaction's connection
void record(Order o) {
jdbc.sql("insert into audit(order_id, action) values (?, ?)")
.params(o.id(), "PLACED").update();
}
}
// each request now holds exactly 1 connection for its whole transaction

If the helper really must use raw JDBC, DataSourceUtils.getConnection(dataSource) returns the connection bound to the current transaction rather than a new one. And if the audit write does not need to be in the same transaction at all, we can restructure so it runs after the first transaction commits, for example from an after-commit event listener, so the request never holds two connections at the same moment.

5Fix 2: Catch It in Tests With a Tiny Pool

Deadlocks like this hide behind spare capacity, so the test should remove the spare capacity. Running the integration tests with a pool of one connection turns any code path that needs two connections at once into an immediate failure, without any load.

Java
# src/test/resources/application-test.properties
spring.datasource.hikari.maximum-pool-size=1
spring.datasource.hikari.connection-timeout=2000 # fail in 2 s instead of 30 s
// a test of placeOrder now fails with a connection timeout
// if any code under it asks for a second connection

This is cheap enough to run on every build. Hikari's own guidance gives the general sizing rule for code that cannot avoid multiple connections: with T threads that may each need C connections at once, a pool of T x (C - 1) + 1 cannot deadlock. For 10 threads needing 2 each, that is 11. But fixing the code is better than sizing around it.

6Watch for REQUIRES_NEW

The most common source of a hidden second connection in Spring is Propagation.REQUIRES_NEW. A method annotated with @Transactional(propagation = REQUIRES_NEW) suspends the caller's transaction, which keeps its connection, and starts a new transaction on a new connection. That is the same pattern as the helper above, wearing an annotation. REQUIRES_NEW is sometimes the right choice, for example when the audit record must survive a rollback of the main transaction, but each use should be checked against the pool size and the concurrency of its callers.

7Follow-up questions

Would a larger pool solve it?

It raises the number of concurrent requests needed to trigger it, but under a big enough spike the same thing happens. The fix is to make each request need only one connection at a time.

Why does the service recover by itself after a while?

Because connectionTimeout breaks the cycle. The waiting requests fail, their transactions roll back, and their connections return to the pool. Until the next burst, new requests succeed. That pattern of freezes followed by waves of timeouts is a strong hint of a pool deadlock.

Can the same thing happen with other pools?

Yes, with any bounded resource that a task holds while asking for more of the same resource: HTTP client connection pools when a call needs a second call to the same host, thread pools when tasks wait for subtasks on the same pool, or semaphores acquired twice. The reasoning and the fixes are the same.

8Conclusion

In this answer, we saw that when each request holds one pooled connection and asks for a second from the same pool of 10, ten concurrent requests use every connection and then wait forever for one more, until connectionTimeout breaks the cycle. It is a resource deadlock on the pool, visible as threads waiting in getConnection rather than on monitors. The fixes are to reuse the caller's connection or transaction, or to hold only one connection at a time; a test profile with a pool of one catches it early, and REQUIRES_NEW deserves attention because it opens a second connection.

What to say in the interviewAbout 90 seconds aloud

Open with

This is a deadlock on the connection pool rather than on locks. Each request's transaction takes one connection and keeps it until the transaction ends. Then the helper asks the same Hikari pool for a second connection.

Then, if they want more

With up to nine concurrent requests there is always one spare, so it works. When ten requests arrive together, they take all ten connections for their transactions, then each waits for a second one.

A connection only comes back when a transaction finishes, and none can finish without its second connection, so they all wait until connectionTimeout, 30 seconds by default, fires and they roll back.

In a thread dump there is no Java-level deadlock; the threads are waiting in HikariPool.getConnection, with our helper just below, and Hikari shows active at maximum, idle zero and pending growing while the database is idle.

The fix is to make each request use one connection: the helper should join the caller's transaction, for example by using JdbcClient or a repository, or DataSourceUtils for raw JDBC, or the work should be restructured so the second step runs after the first commits.

I would catch this class of bug with a test profile that sets the pool to one connection, so any path needing two fails immediately. And I would review REQUIRES_NEW propagation, because it suspends the outer transaction and opens a second connection, which is exactly this pattern.

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.
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 "Each request in a service opens a transaction with one connection and then calls a...": https://scrivio-interview-library.netlify.app/java/concurrency-problems/3bc43bc357/