I am encountering an issue with HikariCP in my Spring Boot application. I keep seeing the warning message:
“Apparent connection leak detected.”
It seems that a database connection remains open for longer than necessary. After some investigation, I realized that the issue might be related to Spring’s Open-Session-In-View (OSIV) pattern. Unfortunately, disabling OSIV isn’t a quick option for us due to the current setup and timeline constraints.
Here’s a simplified example of the method where the issue occurs:
public void example(String id) throws IOException, InterruptedException {
ExampleDto example = exampleService.fetchExample(id);
// The connection should ideally be returned to the pool here,
// but it remains open until after the S3 download is completed
File file = downloadService.downloadByS3Key(bucketName, example.getS3Key());
}
In this scenario:
fetchExample(id) retrieves data from the database using a connection from the Hikari pool.
However, the connection isn’t released until the method completes, which includes the potentially long-running S3 file download.
This behavior results in connections being held longer than necessary, eventually leading to the “connection leak detected” warning.
Question
Is there a temporary workaround or hotfix to ensure that the database connection is returned to the pool as soon as the data retrieval is completed?
I know disabling OSIV entirely could be a permanent solution, but I need something faster to mitigate this issue in the short term.