Java 17: How to Properly Handle Database Connection Pooling with HikariCP to Avoid Memory Leaks
I've looked through the documentation and I'm still confused about I'm currently using HikariCP for database connection pooling in my Java 17 application, and I've run into a problem with what seems to be memory leaks when the app runs for extended periods. I've set up the pool with standard settings, but I noticed that the `HikariDataSource` instances aren't being garbage collected as expected. I've tried ensuring that connections are closed properly in all scenarios. Here's a simplified version of how I'm configuring HikariCP: ```java import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; public class DatabaseConfig { private static HikariDataSource dataSource; public static void init() { HikariConfig config = new HikariConfig(); config.setJdbcUrl("jdbc:mysql://localhost:3306/mydb"); config.setUsername("user"); config.setPassword("password"); config.setMaximumPoolSize(10); config.setConnectionTimeout(30000); dataSource = new HikariDataSource(config); } public static HikariDataSource getDataSource() { return dataSource; } } ``` I call `DatabaseConfig.init()` at the application startup, but I never explicitly shut down the data source, as I'm using it throughout the application lifecycle. I've also checked that I am using `try-with-resources` for connection management: ```java try (Connection conn = DatabaseConfig.getDataSource().getConnection()) { // Perform database operations } ``` Despite this, after a few hours of running the application, I notice a significant increase in memory usage, and the profiler indicates that instances of `HikariDataSource` and `Connection` objects are still in memory. I suspect that the connections are not being released properly, but I'm unsure how to pinpoint the issue. Could there be anything in my configuration or usage patterns that might lead to these objects not being garbage collected? What are the best practices for using HikariCP to ensure that everything is cleaned up properly? Any help would be greatly appreciated! My development environment is macOS. What's the best practice here?