Introduction
Opening a database connection for every operation is expensive. Connection pools reduce that overhead by maintaining reusable connections, but a poorly configured pool can become a source of latency and outages.
A connection pool is a shared resource with a finite capacity. The goal is to configure it according to application concurrency and database limits, not simply to maximize the connection count.
1. Understand what the pool manages
A pool typically manages:
- Connection creation and reuse
- Maximum concurrent connections
- Idle connection handling
- Connection acquisition timeouts
- Connection validation and lifecycle
In many Spring Boot applications, HikariCP is the default JDBC connection pool. Its settings can be configured through application properties.
2. Configure limits deliberately
For example:
spring.datasource.hikari.maximum-pool-size=15
spring.datasource.hikari.connection-timeout=3000
spring.datasource.hikari.minimum-idle=5
These values are illustrative, not universal recommendations. A maximum pool size of 15 may be suitable for one service and excessive for another.
The connection timeout controls how long a caller waits to acquire a connection before failing. It does not control how long the SQL query itself is allowed to execute.
3. Account for all application instances
Suppose an application has six replicas, each configured for a maximum of 15 connections. In principle, those replicas could request up to 90 database connections, before accounting for other applications, migration jobs, or administrative sessions.
Your database must be able to support the combined demand.
A larger pool does not necessarily improve throughput. Too many active queries can increase database contention, memory consumption, and query latency.
4. Diagnose pool saturation
When requests wait for connections, investigate why connections are unavailable.
Potential causes include:
- Queries that take too long
- Transactions held open unnecessarily
- Connections not released correctly
- Sudden traffic spikes
- Excessive application concurrency
- A database operating near capacity
Monitor active, idle, and pending connection counts alongside query duration and request latency. Pool metrics are most useful when correlated with database telemetry.
5. Keep transactions and connections short-lived
Avoid holding a connection while performing unrelated work, such as calling an external API or waiting for user input.
Use try-with-resources or the relevant framework-managed transaction mechanism. Framework-managed connections should not be manually closed outside the lifecycle expected by the framework.
Also configure query timeouts and review slow-query logs. Increasing the pool size can hide the symptom temporarily while leaving the underlying bottleneck untouched.
Conclusion
Connection pooling improves reuse, but the pool must fit the application’s concurrency and the database’s capacity. Measure acquisition delays, active connections, and query duration before changing limits, and consider the combined connection demand across all application instances.