Introduction
HikariCP is a production-grade JDBC connection pooling library for Java. It focuses on correctness, simplicity, and raw speed, consistently outperforming alternatives like Apache DBCP2 and C3P0 in benchmarks. It is the default connection pool in Spring Boot.
What HikariCP Does
- Manages reusable JDBC connections to avoid per-query open/close overhead
- Validates connections before handing them out, preventing stale-connection errors
- Provides configurable pool sizing with minimum idle and maximum pool limits
- Supports connection leak detection with configurable timeout alerts
- Integrates with JMX and Micrometer for runtime pool metrics
Architecture Overview
HikariCP uses a custom lock-free ConcurrentBag collection for pooled connections, bypassing traditional blocking queues. It avoids object allocation on the hot path and uses bytecode-level optimizations via Javassist to generate fast proxy classes for Connection, Statement, and ResultSet objects. A background housekeeper thread handles idle connection retirement and keepalive queries.
Self-Hosting & Configuration
- Add the HikariCP Maven or Gradle dependency to your project
- Configure via HikariConfig object or a hikari.properties file
- Set minimumIdle, maximumPoolSize, connectionTimeout, and idleTimeout
- Enable leak detection with leakDetectionThreshold (recommended: 2000ms in dev)
- Use connectionTestQuery only for drivers that lack JDBC4 isValid() support
Key Features
- Sub-microsecond getConnection() latency under contention
- Automatic connection validation without per-query overhead via JDBC4 isValid
- Built-in connection leak detection with stack trace logging
- JMX and Micrometer metrics for pool utilization and wait times
- Minimal footprint: SLF4J as the only dependency, ~130KB JAR
Comparison with Similar Tools
- Apache DBCP2 — more configuration knobs but higher latency and memory use
- C3P0 — mature but complex config and slower failure recovery
- Tomcat JDBC Pool — tied to Tomcat ecosystem; HikariCP runs anywhere
- Vibur DBCP — similar goals but smaller community and less adoption
- Agroal — Quarkus default; HikariCP is the Spring Boot default
FAQ
Q: Why is HikariCP the default in Spring Boot? A: Its low latency, small footprint, and reliable connection validation make it a safe default for most applications.
Q: How do I size the pool? A: A common formula is connections = (core_count * 2) + effective_spindle_count. For most apps with SSDs, 10-20 connections per node is a good start.
Q: Does it support read/write splitting? A: No. HikariCP is a connection pool, not a proxy. Use application-level routing or a proxy like ProxySQL.
Q: Which databases does it support? A: Any database with a JDBC driver: PostgreSQL, MySQL, Oracle, SQL Server, H2, and more.