Video in progress

How do you spot a connection leak or an undersized pool from HikariCP metrics?

If hikaricp_connections_pending grows and active is always equal to max, the pool is jammed. If active doesn't go back down even when traffic drops, that is a leak. To confirm, you turn on leakDetectionThreshold and take the stack trace from the log.

📖 Answer

Spring Boot exports the Hikari metrics automatically. The main ones:

Metric What it shows
hikaricp_connections_active connections in use right now
hikaricp_connections_idle connections sitting idle
hikaricp_connections_pending threads waiting for a connection
hikaricp_connections_acquire_seconds time to get a connection
hikaricp_connections_timeout_total requests that got no connection after connectionTimeout

You need to tell two pictures apart.

A small pool or a slow query. At peak traffic active = max and pending grows, and after the peak everything returns to normal. Then look at hikaricp_connections_usage_seconds: are connections held for a long time? The cause is often an external HTTP call inside @Transactional.

A leak. active grows like a staircase over the day and doesn't drop at night either. Somebody took a connection and never returned it, for example DataSource.getConnection() without try-with-resources.

spring:
  datasource:
    hikari:
      leak-detection-threshold: 20000  # if held longer than 20s, the stack trace of where it was acquired is logged as WARN

This setting is cheap, and I leave it on in prod too. The threshold just has to be larger than your longest normal transaction, otherwise batch jobs show up as false leaks.