Scrivio Interview library
ScenarioIntermediate6 min read · updated 12 November 2026

After enabling spring.threads.virtual.enabled, a service sees many HikariCP connection timeout errors under the same traffic that worked before. Explain why and what you would change.

Before the change, Tomcat's pool of 200 threads capped how many requests ran at once, and that cap quietly limited how many could wait for a database connection. With virtual threads every request gets its own thread, so far more requests reach the HikariCP pool at the same moment, and any request that waits longer than connectionTimeout fails with a connection timeout error. We fix it by making the limit explicit, for example a semaphore around database work or a cap on concurrent requests, sizing the pool from what the database can handle, and shortening transactions so each connection is held for less time.

Why this question is asked

This is the most common surprise after enabling virtual threads. The traffic is the same, the code is the same, and yet a service that was healthy starts throwing errors from its connection pool. It is a good interview question because the answer shows whether we understand that thread pools were never just about threads: they were also a limit on everything behind them. In this answer, we will recap how the pool and the server interact, show why the old setup hid the problem, reproduce the error, and then go through the fixes in the order I would apply them.

1A Quick Recap of the Two Limits

HikariCP is the connection pool Spring Boot uses by default. It keeps a fixed number of open database connections, 10 by default (maximumPoolSize), and hands one to each caller that asks. If none is free, the caller waits. If it waits longer than connectionTimeout, 30 seconds by default, Hikari gives up and throws an exception.

Tomcat, on the other side, ran each request on a platform thread from a pool of at most 200 (server.tomcat.threads.max). Requests beyond that waited in Tomcat's accept queue, before any of our code ran. So at most 200 requests could ever be inside the application asking for a connection.

2Why the Old Setup Hid the Problem

Suppose each request needs a connection for 50 ms and the service receives 1,000 requests per second during a spike. Ten connections can serve at most 10 / 0.05 = 200 requests per second. Before, at most 200 requests were inside the app, so at most 190 waited at the pool; the rest waited in Tomcat's queue, where they did not count against connectionTimeout, and Tomcat's queue is where the backlog went.

With virtual threads, Tomcat no longer has a 200-thread cap. Every accepted request starts its own virtual thread, runs our controller and immediately asks Hikari for a connection. Now thousands of requests wait in Hikari's queue instead of Tomcat's, and they all start their 30-second timer at the same time. The pool was never bigger than 10; what changed is who waits, and where.

Where requests wait during a spike
SetupRunning in the appWaiting for a connectionResult
Platform threads, Tomcat max 200up to 200up to 190slow, but the rest queue in Tomcat
Virtual threads, no limitthousandsthousandswaits pass 30 s, timeout errors
Virtual threads + semaphore of 30thousandsup to 20, others wait at the semaphorebounded, fails fast if needed
The pool size never changed. Virtual threads moved the queue from Tomcat to Hikari.

3What the Error Looks Like

The errors come from the pool, not from the database. In the logs we typically see something like the following, and Hikari's metrics show the reason clearly.

Text
java.sql.SQLTransientConnectionException: HikariPool-1 - Connection is not available,
request timed out after 30000ms (total=10, active=10, idle=0, waiting=1834)
# metrics during the spike
hikaricp.connections.active 10
hikaricp.connections.pending 1834 # was below 200 before the change

Notice the pending count. It is far above anything Tomcat's thread pool would have allowed, which confirms that the limit moved. The database itself may look relaxed, because it still sees only 10 connections.

4Fix 1: Limit Concurrency Explicitly

The thread pool used to be our limit by accident. With virtual threads we add the limit on purpose, around the resource that is scarce. A Semaphore, a counter of permits that threads acquire and release, is the simplest tool, and it is cheap for virtual threads to wait on.

Java
private final Semaphore dbPermits = new Semaphore(30);
Order load(long id) throws InterruptedException {
if (!dbPermits.tryAcquire(2, TimeUnit.SECONDS)) {
throw new ResponseStatusException(HttpStatus.SERVICE_UNAVAILABLE); // shed load quickly
}
try {
return orderRepository.findById(id).orElseThrow();
} finally {
dbPermits.release();
}
}

The reason we choose a small wait, here 2 seconds, is that an answer in 2 seconds or a fast 503 is far better for callers than a 30-second wait that then fails. A broader alternative is to limit concurrent requests at the edge, for example with a filter or with server limits, but limiting around the database protects the scarce resource directly and leaves requests that do not need the database unaffected.

5Fix 2: Size the Pool From the Database, Not From Traffic

It is tempting to raise maximumPoolSize to 500. That moves the queue into the database, which then runs 500 queries at once on, say, 8 cores and becomes slower for everyone. A database runs efficiently with a limited number of active connections, roughly a small multiple of its cores, and that budget is shared by every instance of every service. So if the database handles about 40 active connections well and the service runs 4 instances, each instance gets a pool of about 10, and the semaphore sits slightly above that.

6Fix 3: Hold Connections for Less Time

Throughput of the pool is pool size divided by how long each request holds a connection. Halving the hold time from 50 ms to 25 ms doubles capacity without touching the database. The usual culprits are transactions that are too wide: an @Transactional method that calls a remote HTTP service, renders a document or waits on a message broker while holding its connection. Moving that work outside the transaction, and keeping the transaction to the queries and updates themselves, often fixes the problem with no other change. Notice that this was always a good idea; virtual threads just made its absence visible.

7Follow-up questions

Could I just raise connectionTimeout?

That hides the error but not the problem. Requests would wait even longer, holding memory and keeping clients waiting, and during a sustained spike the queue keeps growing. A short wait at an explicit limit, with a clear 503 when it is full, is easier to reason about and recover from.

Why not set a lower maximum on Tomcat instead?

With virtual threads, Tomcat's thread maximum does not apply, because each request gets a new virtual thread rather than one from the pool. We can limit concurrent connections or requests at the server or in a filter, which restores a global cap, but it also limits requests that never touch the database. A limit around the database is more precise.

What should I load test before enabling virtual threads?

Run the same spike as production with the property on and off, and compare pending connections in Hikari, error rates and latency percentiles. Also check every other pooled resource, such as HTTP client connection pools and downstream rate limits, because the same shift of the queue happens there.

8Conclusion

In this answer, we saw that Tomcat's 200-thread pool used to cap how many requests were inside the application, and with them how many waited for one of Hikari's 10 connections. Virtual threads remove that cap, so the same traffic produces thousands of waiters at the pool, and waits beyond connectionTimeout become errors. We fixed it by limiting concurrency explicitly with a semaphore or a request limit, sizing the pool from the database's capacity across all instances, and shortening transactions so each connection is held for less time.

What to say in the interviewAbout 80 seconds aloud

Open with

The traffic did not change; where requests wait did. Before, Tomcat ran requests on a pool of 200 platform threads, so at most 200 requests were inside the app, and only those could queue for Hikari's connections, 10 by default.

Then, if they want more

Everything else waited in Tomcat's queue, which did not count against Hikari's 30-second connectionTimeout. So the thread pool was silently limiting database concurrency. With virtual threads, every accepted request gets its own thread and goes straight to the pool.

During a spike, thousands of requests wait for those same 10 connections, start their timers at the same moment, and the ones that wait more than 30 seconds fail with SQLTransientConnectionException, connection is not available. Hikari's pending metric shows it clearly. I would not just make the pool huge, because that moves the queue into the database.

Instead, first I make the limit explicit: a semaphore around database work, with a short tryAcquire timeout and a 503 when it is full, or a limit on concurrent requests. Second, I size the pool from what the database can handle across all instances.

Third, I shorten transactions, for example by moving remote calls out of @Transactional methods, because halving the hold time doubles the pool's throughput.

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
JEP 444, Virtual Threads
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 "After enabling spring.threads.virtual.enabled, a service sees many HikariCP connection...": https://scrivio-interview-library.netlify.app/java/virtual-threads/e2cb167722/