I use connection pool from HikariCP. HikariCP after every request takes connection to pool back, but ain't closing it, so I have idle connections in DB, which I have to close by the hand (for example).
How can I properly close pool after every request, because I don't need constant pools actively?
Here is my class which connects to DB:
public class ReportRequester {
private final JdbcTemplate jdbcTemplate;
public List<List<Object>> request(String request) {
return jdbcTemplate.execute((Connection connection) -> {
List<List<Object>> objectList = new ArrayList<>();
try (PreparedStatement ps = connection.prepareStatement(request);
ResultSet rs = ps.executeQuery()) {
ResultSetMetaData resultSetMetaData = rs.getMetaData();
while (rs.next()) {
List<Object> row = new ArrayList<>();
for (int i = 1; i <= resultSetMetaData.getColumnCount(); ++i) {
row.add(rs.getObject(i));
}
objectList.add(row);
}
} catch (SQLException e) {
// обработка исключения
e.printStackTrace();
}
log.info("Connection has been closed");
return objectList;
});
}
}
I tried to use
spring.datasource.hikari.idle-timeout= 60000,
but realised it doesn't close pool. How can I configure closing connection pool? I know it's not recommended, but unfortunately I need to.
I tried to use try-with-resources but it also didn't give my anything.