BackendBench
Coffee

Home โ€บ Java exceptions โ€บ SQLTransientConnectionException

Checked exception ยท java.sql

SQLTransientConnectionException: causes and fixes

HikariCP could not give out a connection within the timeout: the pool is exhausted or the database is unreachable.

What it looks like

java.sql.SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 30000ms

Extends SQLTransientException in java.sql.

What it means

Every request that needs the database borrows a connection from the pool. If all connections stay busy for longer than connectionTimeout (30 seconds by default) the borrower gives up with this exception. Either the pool is too small for the load, or connections are held too long (slow queries, transactions open during remote calls), or they are leaked and never returned. Newer HikariCP versions include the pool state in the message: total, active, idle and waiting.

Common causes

How to fix it

  1. Turn on leak-detection-threshold to log the stack trace of code that holds a connection too long.
  2. Shorten transactions and move remote calls outside them; disable open-in-view.
  3. Size the pool from measured load; see the HikariCP pool size calculator.
  4. Check database health and max_connections across all instances.

Broken and fixed

fails
@Transactional
public void process() {
    var data = repo.load();
    httpClient.send(...); // connection held during the remote call
}
works
public void process() {
    var data = loader.load();      // short transaction
    var result = httpClient.send(...); // no connection held
    writer.save(result);           // another short transaction
}

๐Ÿ”Ž Unravel the failing code ๐Ÿ”Ž Unravel the fix

In Spring and enterprise code

If the pool shows active equal to total with many waiting, it is saturation or leaks; if active is low, the database is probably unreachable.

Debug it faster

Paste the whole stack trace into the stack trace beautifier to hide framework frames, jump to the root cause and find the first line of your own code. It runs in your browser, so the trace never leaves your machine.

Frequently asked questions

Is SQLTransientConnectionException a checked or unchecked exception?

SQLTransientConnectionException is a checked exception (it extends SQLTransientException), so the compiler forces you to catch it or declare it with throws.

Which package is SQLTransientConnectionException in?

It lives in java.sql, so the import is import java.sql.SQLTransientConnectionException;.

What is the first thing to check when I see SQLTransientConnectionException?

Read the message and the first stack trace line in your own code. The usual causes are: Connection leaks: a connection obtained manually and never closed, or a transaction left open; and Slow queries or long transactions holding connections.

Related exceptions

More tools you might need