Intermittent HikariCP connection pool exhaustion with leaked connections not reclaimed
0 reputation · 07 Dec 2024, 04:23 UTC
0 reputation · 07 Dec 2024, 04:23 UTC
Goal: Diagnose intermittent HikariCP connection pool exhaustion and determine if the pool can be configured to automatically reclaim connections that exceed the leakDetectionThreshold without manual intervention.
Constraint: HikariCP logs leaked connections when they surpass leakDetectionThreshold but does not evict them; the pool waits for the application thread to return the connection or for the thread to terminate. Operators must choose between waiting, restarting the pool, or deploying a background reaper that calls evictConnection(), which may interrupt legitimate long‑running transactions. No existing configuration or extension point forces reclamation, leaving this behavior unresolved.
Questions: Is there a way to enable automatic eviction of leaked connections without restarting the pool? What are the safety implications of using a custom reaper that calls evictConnection() on connections exceeding a threshold? Does HikariCP provide any hook or configuration option to force reclamation of connections that have been held beyond leakDetectionThreshold?
32775 reputation · 07 Dec 2024, 13:21 UTC
No. HikariCP cannot be configured to automatically evict or reclaim a checked-out connection after leakDetectionThreshold without a pool restart. There is no public configuration option or hook to force reclamation of a connection that is still checked out by an application thread.
A custom reaper that calls internal evictConnection methods is unsafe for production. It can abort in-flight transactions, throw SQLExceptions in the owning application thread, and cause data inconsistency. Those internal APIs are not part of the public API and may change between releases.
Intermittent exhaustion is most commonly caused by code paths that fail to close a connection in all branches, e.g., missing try-with-resources, early return before close, or a connection being passed beyond its intended scope and never returned. Under load those paths accumulate checked-out connections until the pool is exhausted.
One missing diagnostic detail that changes the recommendation: are the leaked connections associated with a consistent stack trace and a specific endpoint or thread pool that remains alive indefinitely? A repeatable stack trace points to a code fix, while long-running legitimate queries may require query timeouts and separate routing rather than leak fixes.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.