Setting Up HikariCP for Production Java Applications
Opening a socket to PostgreSQL for every query costs a TCP handshake, a TLS handshake, authentication, and session setup. That overhead is tolerable once an hour and painful when a request thread needs a connection every few milliseconds. A connection pool keeps a bounded set of connections open and lends them out on demand, which is exactly why HikariCP has become the default behind Spring Boot's JDBC and JPA starters.
HikariCP is a small, bytecode-compiled pool with a fast, low-contention acquisition path, per-connection tracking, configurable validation, and Micrometer metrics. It also fails loudly: when a connection cannot be obtained, you get a message naming the pool and the timeout rather than a mysterious stack trace twenty frames deep.
This walkthrough covers adding the dependency, the configuration properties that matter, how the pool sizes itself against your workload, and how to watch it in production. If the language fundamentals feel shaky, the site's Java 4 guide covers them before you dive into configuration.
For Australian teams, the stakes are familiar. Payment platforms and identity systems built in Sydney and Melbourne face sharp traffic peaks around Boxing Day and the start of the Ashes series, while fintechs operating under the Privacy Act 1988 need Australian data residency and auditable access to customer records. A pool that behaves well in development can still collapse under those conditions if it is sized by guesswork.
Why a JVM service needs pooled connections
A connection is not just an object; it is a live session with a file descriptor, a backend process, and memory on both sides of the wire. An application with twenty request threads that each open their own connection will eventually exceed the database's max_connections limit and watch new logins rejected. That failure looks like an outage rather than a resource leak, which makes it expensive to diagnose.
Pooling puts a hard ceiling on concurrency, and that ceiling is the cheapest backpressure you can build. It also amortises the expensive parts — TLS negotiation and authentication — across thousands of operations instead of thousands of connections. Hibernate, MyBatis, and plain JDBC all benefit, because the pool sits underneath the driver and the ORM never notices.
HikariCP suits this brief because it stays out of the way. It validates idle connections with a lightweight isValid check, supports custom eviction, and exposes the state of every connection through standard instrumentation.
Wiring HikariCP into a Spring Boot service
The spring-boot-starter-jdbc artifact already pulls HikariCP in transitively, so most projects need no extra dependency. If you are assembling your own stack, add it directly:
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>5.1.0</version>
</dependency>
Configuration lives under spring.datasource.hikari, and environment variables keep credentials out of the repository:
spring:
datasource:
url: jdbc:postgresql://db.internal.ap-southeast-2.rds.amazonaws.com:5432/orders
username: app_user
password: ${DB_PASSWORD}
hikari:
maximum-pool-size: 12
minimum-idle: 3
connection-timeout: 3000
validation-timeout: 1000
idle-timeout: 600000
max-lifetime: 1500000
leak-detection-threshold: 60000
With JPA, remember that Hibernate borrows from the Spring-managed DataSource. Adjust the pool through spring.datasource.hikari, not hibernate.connection.pool_size, otherwise you end up with two competing settings and a pool nobody is actually using.
How HikariCP compares to other pooling options
Legacy Spring XML configurations often still carry Apache DBCP2, while older Hibernate applications tend to ship with c3p0. The practical differences are small in code and large in behaviour under load.
| Option | Runtime overhead | Leak diagnostics | Metrics | Where it still fits |
|---|---|---|---|---|
| HikariCP | Very low | Configurable threshold with stack trace | Micrometer, JMX | Spring Boot, JDBC, JPA |
| Apache DBCP2 | Low | Basic | JMX | Long-lived Spring XML setups |
| c3p0 | Noticeable | Basic | JMX | Legacy Hibernate deployments |
No pool (DriverManager) |
Full cost per query | None | None | Batch jobs and CLI tools |
The trade-off is not really about speed. HikariCP wins on observability: it reports pending threads, idle connections, and acquisition latency in the same format as the rest of your Spring Boot metrics, so a saturated pool appears on a dashboard next to CPU and garbage collection instead of hiding inside application logs.
Sizing the pool and setting timeouts
A common starting point is roughly twice your available CPU cores, plus any physical spindle the database still depends on. In a container with two vCPUs, that lands near four connections; add headroom for reporting queries and you might settle on eight to twelve. Keep the total across every instance below the database limit, since three application servers with twenty connections each will still exhaust a Postgres instance configured for the default hundred.
Timeouts matter as much as the size. connection-timeout measures how long a thread waits for a connection from the pool, not how long the driver takes to reach the database, so three seconds is a sensible value for request-serving code. max-lifetime must sit below the database's own idle cutoff — Amazon RDS for PostgreSQL closes idle client connections after thirty minutes, so a lifetime of 1,500,000 milliseconds keeps the pool rotating ahead of the server. leak-detection-threshold logs the stack trace of the borrower when a connection is held too long, which catches the classic forgotten ResultSet or an unclosed transaction.
Watching the pool and catching leaks in production
Expose /actuator/metrics and watch hikaricp_connections_active, hikaricp_connections_pending, and hikaricp_connections_acquire_seconds. A pending count that stays above zero during business hours means the pool is too small or a query is holding connections longer than it should; an active count that never returns to idle points to a leak rather than to traffic.
Real leaks usually come from code that borrows a connection and never returns it — a stream left open, a ResultSet iterated outside a try-with-resources block, or a batch job that forgets to commit. Set the leak threshold to a minute in staging, read the logged stack trace, and fix the borrower instead of raising the timeout.
Local deployment details shape the last mile. Keeping customer data inside Australian regions — Sydney's ap-southeast-2 or Melbourne's ap-southeast-4 — satisfies residency expectations without adding meaningful latency between a Melbourne service and a Sydney database. Teams in Perth and Brisbane running the same stack against legacy MySQL instances can keep the identical Hikari configuration and change only the JDBC URL, which makes migration a configuration task rather than a rewrite.