springboot-How to solve java.sql.SQLTransientConnectionException: HikariPool Connection is not available, request timed out after 3600000ms
1. Introduction
When we use HikariCP connection pool in SpringBoot applications, sometimes, we would encounter this error:
2. Environments
SpringBoot 1.x and 2.x
3. The code
The SpringBoot application.properties
Here we use a HikariCP connection pool with connection size from 5 to 20.
The SpringBoot command:
Pay attention to the line jdbcTemplate.getDataSource().getConnection().getMetaData().getDatabaseProductName(), and the line jdbcTemplate.getDataSource().getConnection().close().
First, we retrieve a connection from jdbcTemplate’s datasource , then we close it by call jdbcTemplate.getDataSource().getConnection().close().
When we run the code, we get this error:
4. The solution
We can solve this problem by these ways:
Get and close specific connection
In the above code sample, we can see that we get a connection from the datasource and then close a connection from the datasource, they may be different connection! e.g. We get a connection and close another one.
We can change the code as follows:
5. Summary
When we use a connection pool in SpringBoot Applications, pay attention to the connection leak problem, if it occurs, try to check if you have close the right connection when you use it.