Connection pool misconfiguration is one of the top causes of Spring Boot performance issues. Learn how to size, configure, and monitor HikariCP correctly for production.
JOptimize Team
HikariCP is the default connection pool in Spring Boot, and it's fast by design. But the default configuration is sized for development, not production. A misconfigured pool causes connection timeouts, thread starvation, and cascading failures under load - even when your database is perfectly healthy.
Every request that touches the database needs a connection from the pool. If all connections are in use, incoming requests queue up waiting. If the queue fills, requests fail with:
com.zaxxer.hikari.pool.HikariPool$PoolInitializationException java.sql.SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 30000ms
The default maximum-pool-size in HikariCP is 10. For a service handling hundreds of concurrent requests, this will cause problems.
The HikariCP team references a well-known formula: you don't need as many connections as you think.
pool_size = (core_count * 2) + effective_spindle_count # 4-core server with SSD (spindle_count = 1): pool_size = (4 * 2) + 1 = 9 ? 10 # 16-core server hitting PostgreSQL: pool_size = (16 * 2) + 1 = 33
More connections don't mean more throughput - they increase context switching and lock contention on the database. A smaller pool that keeps the DB busy is faster than a large pool that queues at the DB level.
# application.properties - production-ready HikariCP config # Pool sizing spring.datasource.hikari.maximum-pool-size=20 spring.datasource.hikari.minimum-idle=5 # Connection acquisition - fail fast instead of hanging 30s spring.datasource.hikari.connection-timeout=3000 # Keep-alive (prevents stale connections) spring.datasource.hikari.keepalive-time=30000 spring.datasource.hikari.max-lifetime=1800000 spring.datasource.hikari.idle-timeout=600000 # Pool name - visible in logs and metrics spring.datasource.hikari.pool-name=MyAppPool
wait_timeout (MySQL default: 8h). Critical for cloud databases that drop idle connections.maximum-pool-size for a fixed pool (HikariCP's recommendation for best performance).Add these dependencies to expose HikariCP metrics via Micrometer:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> </dependency>
management.endpoints.web.exposure.include=health,metrics,prometheus management.metrics.enable.hikaricp=true
Key metrics to watch at /actuator/metrics:
| Metric | Alert threshold |
|---|---|
hikaricp.connections.active | Approaching maximum-pool-size |
hikaricp.connections.pending | > 0 sustained |
hikaricp.connections.timeout.total | Any value > 0 |
hikaricp.connections.acquire (p99) | > 5ms |
If your app has multiple data sources (primary + read replica), configure separate pools:
@Configuration public class DataSourceConfig { @Bean @ConfigurationProperties("spring.datasource.primary.hikari") public HikariDataSource primaryDataSource() { return new HikariDataSource(); } @Bean @ConfigurationProperties("spring.datasource.replica.hikari") public HikariDataSource replicaDataSource() { return new HikariDataSource(); } }
max-lifetime, you get stale connection errors at randommax_connections=100 and you run 5 instances each with pool size 50, you'll get connection refused from the DB itselfHikariCP is fast out of the box, but production requires deliberate tuning. Size the pool based on the CPU-core formula, set a short connection-timeout to fail fast, configure max-lifetime to survive cloud database connection resets, and add Actuator metrics to catch pool exhaustion before users do.
JOptimize scans your Spring Boot project for patterns that stress the connection pool - unbounded queries, missing pagination, missing transaction boundaries - and prioritizes them by impact.
Spot database bottlenecks before your pool exhausts - free scan, no configuration required.
Master Spring Boot, security, and Java performance with hands-on courses.
JOptimize finds N+1 queries, EAGER collections, and 70+ other issues in your Java codebase — in under 30 seconds.