HikariCP
HikariCP is the default connection pool in Spring Boot 2.x+. Known for its speed, reliability, and minimal overhead, it is widely considered the best JDBC connection pool for Java applications.
Why Connection Pooling?
Creating a JDBC connection is expensive (TCP handshake, authentication, memory allocation). A pool reuses connections:
Application Thread → Connection Pool → Database
↕ reuse
[conn1, conn2, conn3...]
Spring Boot Default
Spring Boot auto-configures HikariCP when spring-boot-starter-jdbc or spring-boot-starter-data-jpa is on the classpath:
spring:
datasource:
url: jdbc:postgresql://localhost:5432/mydb
username: postgres
password: secret
driver-class-name: org.postgresql.Driver
hikari:
pool-name: MyAppPool
maximum-pool-size: 20
minimum-idle: 5
connection-timeout: 30000 # 30s wait for connection
idle-timeout: 600000 # 10min idle before retire
max-lifetime: 1800000 # 30min max connection age
leak-detection-threshold: 60000 # warn if held > 60s
Key Configuration
| Property | Default | Description |
|---|---|---|
maximum-pool-size |
10 | Max connections in pool |
minimum-idle |
same as max | Min idle connections |
connection-timeout |
30000ms | Max wait for a connection |
idle-timeout |
600000ms | Idle connection retirement |
max-lifetime |
1800000ms | Max connection lifetime |
leak-detection-threshold |
0 (off) | Warn on connection leaks |
Pool Sizing Formula
connections = ((core_count * 2) + effective_spindle_count)
For a 4-core server with SSD:
connections = (4 * 2) + 1 = 9 → round to 10
More connections ≠ better performance. PostgreSQL performs best with 10–20 connections per application instance.
Monitoring
@RestController
public class PoolMetricsController {
private final HikariDataSource dataSource;
@GetMapping("/pool/stats")
public Map<String, Object> poolStats() {
HikariPoolMXBean pool = dataSource.getHikariPoolMXBean();
return Map.of(
"active", pool.getActiveConnections(),
"idle", pool.getIdleConnections(),
"total", pool.getTotalConnections(),
"waiting", pool.getThreadsAwaitingConnection()
);
}
}
Via Actuator:
management:
endpoints:
web:
exposure:
include: health,metrics
Metrics available at /actuator/metrics/hikaricp.connections.active.
Connection Validation
HikariCP validates connections on checkout using a lightweight query:
spring:
datasource:
hikari:
connection-test-query: SELECT 1 # only needed for older drivers
Modern JDBC 4+ drivers support Connection.isValid() — no test query needed.
Common Issues
Connection leak:
Apparent connection leak detected
Enable leak detection and ensure connections are closed (use try-with-resources or Spring @Transactional).
Pool exhausted:
Connection is not available, request timed out after 30000ms
Increase maximum-pool-size or investigate slow queries holding connections.
Best Practices
- Use Spring Boot defaults — HikariCP is pre-configured optimally
- Size pool based on CPU cores, not arbitrary large numbers
- Enable
leak-detection-thresholdin development - Set
max-lifetimebelow database connection timeout - Monitor active/idle/waiting metrics in production