HikariCP
Spring Boot
Logging
Database Connection Pooling
Java

How to enable HikariCP logging in Spring Boot

Codemia Knowledge Hub ·

Set the com.zaxxer.hikari logger to DEBUG in application.properties or application.yml. HikariCP is Spring Boot's default connection pool, and at DEBUG it logs pool statistics (total, active, idle, waiting), connection lifecycle events and timeout failures, which is what you need to diagnose slow queries, connection leaks and pool exhaustion.

Turning on HikariCP debug logging

application.properties

properties
1# Enable HikariCP debug logging
2logging.level.com.zaxxer.hikari=DEBUG
3logging.level.com.zaxxer.hikari.HikariConfig=DEBUG
4
5# For detailed pool statistics
6logging.level.com.zaxxer.hikari.pool.HikariPool=DEBUG

application.yml

yaml
1logging:
2  level:
3    com.zaxxer.hikari: DEBUG
4    com.zaxxer.hikari.HikariConfig: DEBUG
5    com.zaxxer.hikari.pool.HikariPool: DEBUG

Use DEBUG for troubleshooting and INFO in production. HikariCP's TRACE output is extremely verbose and hurts performance.

Reading the pool stats lines

 
DEBUG HikariPool - HikariPool-1 - Pool stats (total=10, active=3, idle=7, waiting=0)
DEBUG HikariPool - HikariPool-1 - Added connection conn5
DEBUG HikariPool - HikariPool-1 - Timeout failure stats (total=10, active=10, idle=0, waiting=5)
Log EntryMeaning
total=10Total connections in the pool
active=3Connections currently in use
idle=7Connections available for use
waiting=0Threads waiting for a connection
Added connectionNew connection was created
Timeout failureThread could not get a connection in time

Watch waiting. A non-zero value means the pool is too small or connections are held too long, and it shows up before timeout errors do.

Pool settings that affect the logs

properties
1# application.properties
2spring.datasource.hikari.maximum-pool-size=20
3spring.datasource.hikari.minimum-idle=5
4spring.datasource.hikari.connection-timeout=30000
5spring.datasource.hikari.idle-timeout=600000
6spring.datasource.hikari.max-lifetime=1800000
7spring.datasource.hikari.pool-name=MyAppPool
yaml
1# application.yml
2spring:
3  datasource:
4    hikari:
5      maximum-pool-size: 20
6      minimum-idle: 5
7      connection-timeout: 30000    # 30 seconds
8      idle-timeout: 600000         # 10 minutes
9      max-lifetime: 1800000        # 30 minutes
10      pool-name: MyAppPool

pool-name makes log lines easy to tell apart when there are several pools.

A bigger pool is not always better: each connection uses memory in both the application and the database. Start with maximum-pool-size = (2 * CPU cores) + number_of_disks and tune it with load testing.

connection-timeout is how long a thread waits to get a connection from the pool. It is not a query timeout; that is configured separately, via spring.jpa.properties.jakarta.persistence.query.timeout.

Logging connection leaks

HikariCP can report connections that are borrowed and never returned:

properties
# Warn if a connection is held for more than 30 seconds
spring.datasource.hikari.leak-detection-threshold=30000
yaml
1spring:
2  datasource:
3    hikari:
4      leak-detection-threshold: 30000  # 30 seconds

When it detects a leak, HikariCP logs a stack trace of where the connection was acquired:

 
1WARN  HikariPool - Connection leak detection triggered for conn7 on thread http-nio-8080-exec-3,
2stack trace follows
3java.lang.Exception: Apparent connection leak detected
4    at com.example.UserRepository.findAll(UserRepository.java:25)
5    at com.example.UserService.getUsers(UserService.java:18)
6    ...

The trace points at the code that took the connection and did not give it back. Without a threshold, connections held indefinitely (for example from a missing @Transactional or an unclosed resource) quietly exhaust the pool, so set it to 30 to 60 seconds in development.

Reading pool metrics through JMX

properties
spring.datasource.hikari.register-mbeans=true
java
1// Access pool metrics programmatically
2import com.zaxxer.hikari.HikariDataSource;
3import com.zaxxer.hikari.HikariPoolMXBean;
4import java.util.Map;
5import javax.sql.DataSource;
6import org.springframework.web.bind.annotation.GetMapping;
7import org.springframework.web.bind.annotation.RestController;
8
9@RestController
10public class PoolMonitorController {
11
12    private final HikariDataSource dataSource;
13
14    public PoolMonitorController(DataSource dataSource) {
15        this.dataSource = (HikariDataSource) dataSource;
16    }
17
18    @GetMapping("/pool-stats")
19    public Map<String, Integer> getPoolStats() {
20        HikariPoolMXBean pool = dataSource.getHikariPoolMXBean();
21        return Map.of(
22            "activeConnections", pool.getActiveConnections(),
23            "idleConnections", pool.getIdleConnections(),
24            "totalConnections", pool.getTotalConnections(),
25            "threadsAwaitingConnection", pool.getThreadsAwaitingConnection()
26        );
27    }
28}

Spring Boot Actuator metrics

xml
1<dependency>
2    <groupId>org.springframework.boot</groupId>
3    <artifactId>spring-boot-starter-actuator</artifactId>
4</dependency>
properties
management.endpoints.web.exposure.include=health,metrics
management.endpoint.health.show-details=always

Query the pool metrics:

bash
1# Health check includes pool status
2curl http://localhost:8080/actuator/health
3
4# HikariCP-specific metrics
5curl http://localhost:8080/actuator/metrics/hikaricp.connections.active
6curl http://localhost:8080/actuator/metrics/hikaricp.connections.idle
7curl http://localhost:8080/actuator/metrics/hikaricp.connections.pending
8curl http://localhost:8080/actuator/metrics/hikaricp.connections.timeout
9curl http://localhost:8080/actuator/metrics/hikaricp.connections.usage

Prometheus and Grafana

properties
# Expose Prometheus endpoint
management.endpoints.web.exposure.include=prometheus
promql
1# Grafana queries for HikariCP monitoring
2hikaricp_connections_active{pool="MyAppPool"}
3hikaricp_connections_idle{pool="MyAppPool"}
4hikaricp_connections_pending{pool="MyAppPool"}
5rate(hikaricp_connections_timeout_total{pool="MyAppPool"}[5m])
6hikaricp_connections_usage_seconds_sum / hikaricp_connections_usage_seconds_count

The key numbers to watch are active connections, idle connections, threads awaiting a connection, and the timeout count.

Logging SQL alongside pool logs

properties
1# Log SQL statements
2logging.level.org.springframework.jdbc.core=DEBUG
3logging.level.org.hibernate.SQL=DEBUG
4
5# Log SQL parameter values (Hibernate 6; on Hibernate 5 use org.hibernate.type.descriptor.sql.BasicBinder)
6logging.level.org.hibernate.orm.jdbc.bind=TRACE
7
8# Combined with HikariCP logs
9logging.level.com.zaxxer.hikari=DEBUG

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design