Scrivio Interview library
DebuggingFoundations6 min read · updated 12 November 2026

A Spring Boot service logs "HikariPool-1 - Connection is not available, request timed out after 30000ms" during peak traffic, while the database itself shows low CPU. What are the likely causes and how would you find the real one?

The message means every connection in the Hikari pool was in use and a request waited the full connectionTimeout, 30 seconds by default, without getting one. Low database CPU tells us the database is not the bottleneck; connections are being held but not used, typically by long transactions that make remote calls, connection leaks, open-in-view keeping a connection for the whole request, or a pool too small for the concurrency. We find the real cause with Hikari's Micrometer metrics and leakDetectionThreshold, and fix the holding pattern before making the pool bigger.

Why this question is asked

This error confuses people because the database looks healthy. It is idle, its CPU is low, yet the service says it cannot get a connection. The key is that a pooled connection can be busy without the database doing anything: it is reserved by a thread that is waiting for something else. Interviewers ask this to see whether we can reason about pools and where time is spent, instead of reaching for a bigger pool. In this answer, we will recap how the pool works, list the usual causes, measure which one we have, and then fix it in the right order.

1A Quick Recap of the Connection Pool

HikariCP keeps a fixed set of open database connections, by default up to 10 (maximumPoolSize). When code needs a connection, it borrows one from the pool, and returns it when done. If none is free, the thread waits. If it still has none after connectionTimeout, 30,000 ms by default, Hikari throws SQLTransientConnectionException with exactly the message in the question.

So the error says nothing about the database being slow. It says all 10 connections were borrowed for longer than requests could wait. With low database CPU, the connections are mostly reserved but idle from the database's point of view.

2Who Is Holding the Connections?

There are four usual suspects. The first is a transaction that makes a remote call. A @Transactional method takes a connection at the start and keeps it until commit, so if it calls a payment service that takes 3 seconds at peak, that connection is reserved for 3 seconds while the database waits.

The second is open-in-view. Spring Boot enables spring.jpa.open-in-view by default (with a warning at startup), which keeps the JPA EntityManager open for the whole web request so lazy loading works in views. Once a request touches the database, its connection can stay attached until the response is written, including time spent calling other services or rendering.

The third is a leak: code that borrows a connection and never returns it, for example a call to DataSource.getConnection() in our own code without try-with-resources, or a stream from a repository that is never closed. The fourth is simply a pool too small for the real concurrency, for example 10 connections for 200 request threads, all doing short but frequent queries.

Step throughA connection held during a remote call
Request threadHikari poolDatabasePayment API
borrow connection (begin transaction)
UPDATE order, 2 ms
charge card, 3 s at peak
ok
commit, 1 ms
return connection

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

The connection is reserved for 3 seconds but the database works for 3 milliseconds.

3Measuring Instead of Guessing

Spring Boot registers Hikari's metrics with Micrometer automatically. The useful ones are hikaricp.connections.active (borrowed now), idle, pending (threads waiting), acquire (how long borrowing takes), usage (how long a connection is held), and timeout (how many requests gave up). During the incident we expect active at the maximum and pending high. The interesting number is usage: if connections are held for 2 seconds on average while queries take 5 ms, something is holding them without using them.

YAML
# application.yml
spring:
datasource:
hikari:
leak-detection-threshold: 10000 # log a stack trace for any connection held over 10 s
# log output
WARN ProxyLeakTask : Connection leak detection triggered for org.postgresql.jdbc.PgConnection@3c1a,
stack trace follows
java.lang.Exception: Apparent connection leak detected
at com.example.OrderService.placeOrder(OrderService.java:48) # the method that borrowed it

leakDetectionThreshold makes Hikari log the stack trace of where a connection was borrowed when it has been out longer than the threshold. Despite the name, it catches long holders as well as true leaks, which is exactly what we want here. On the database side, pg_stat_activity showing many connections in the state idle in transaction confirms that transactions are open but waiting on the application.

4Fixing the Holding Pattern First

Once we know who holds connections, we change that code. Remote calls move outside the transaction: call the payment API first, then open a short transaction to record the result, or record an intent, commit, and call the API afterwards, depending on the business rules. Open-in-view goes off with spring.jpa.open-in-view=false, and any lazy loading that relied on it is replaced by fetching what the view needs inside the service method. Leaks are fixed with try-with-resources or by using JdbcTemplate and repositories, which return connections themselves.

Java
// before: connection held during the remote call
@Transactional
public void placeOrder(Order o) {
orders.save(o);
payments.charge(o); // 3 s with a connection reserved
}
// after: the transaction only covers database work
public void placeOrder(Order o) {
PaymentResult r = payments.charge(o); // no connection held
orderWriter.saveWithPayment(o, r); // @Transactional on another bean, a few ms
}
Step throughFinding the cause
  1. Usage time much longer than query time?
    YesSomething holds connections idleNoPool may be too small
  2. Leak detection shows remote calls?
    YesMove calls out of transactionsNoCheck open-in-view and leaks
  3. Fixed, still pending threads?
    YesSize the pool from the database budgetNoDone

3 steps in order. Play them, or step through them one at a time.

Only after this would we consider the pool size. With connections held for milliseconds instead of seconds, the same 10 connections often serve many times the traffic.

5Follow-up questions

Why can a bigger pool make things worse?

If connections are held idle, a bigger pool only lets more requests pile up in the same slow pattern, and if the database is actually busy, more connections mean more concurrent queries competing for the same CPU cores and disk, which makes each query slower. Many instances each with a bigger pool can also exceed the database's max_connections and break other services. A total across all instances of a few connections per database core is usually enough.

Should we lower connectionTimeout?

Often yes. Waiting 30 seconds for a connection rarely helps a user, and those waiting threads pile up. A timeout of a few seconds fails fast, frees request threads and makes the problem visible sooner, though it does not fix the cause.

How does a virtual-thread application change this?

Virtual threads make it cheap to have thousands of concurrent requests, so many more of them can wait for the same 10 connections. The pool becomes the main concurrency limit, which makes short connection holding even more important.

6Conclusion

In this answer, we saw that the Hikari timeout means every pooled connection was borrowed for longer than the 30 second wait, and that low database CPU points to connections being held, not used. The usual causes are remote calls inside transactions, open-in-view, leaks and a pool too small for the concurrency. We identify the cause with Hikari's active, pending, acquire and usage metrics and leakDetectionThreshold, fix the holding pattern first, and only then size the pool.

What to say in the interviewAbout 80 seconds aloud

Open with

That message means all connections in the pool were borrowed and a request waited the full connectionTimeout, 30 seconds by default, without getting one. Since the database CPU is low, the database is not the bottleneck; connections are being held but not used.

Then, if they want more

My usual suspects are a @Transactional method that calls a remote service while holding its connection, open-in-view, which Spring Boot enables by default and which keeps the connection for the whole request, a connection leak, or simply a pool of 10 connections for far more concurrent requests.

To find the real one I would look at the Hikari metrics in Micrometer: active at the maximum and pending high confirm exhaustion, and if usage time is seconds while queries take milliseconds, something is holding connections idle.

I would enable leakDetectionThreshold, say 10 seconds, which logs the stack trace of where long-held connections were borrowed, and check pg_stat_activity for idle in transaction sessions. Then I fix the holding pattern: move remote calls out of transactions, set spring.jpa.open-in-view to false and fetch what views need, and close resources properly.

Only after that would I adjust the pool size, because a bigger pool usually just lets the same problem grow and can overload the database.

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 "A Spring Boot service logs "HikariPool-1 - Connection is not available, request timed...": https://scrivio-interview-library.netlify.app/databases/database-scaling/2948d05a44/