How to resolve java.sql.SQLTransientConnectionException: HikariPool Connection is not available, request timed out after 3600000ms
1. Introduction
When using the HikariCP connection pool in Spring Boot applications, you might encounter the following error:
INFO | jvm 1 | 2019/07/15 09:10:00 | java.sql.SQLTransientConnectionException: HikariPool-2 - Connection is not available, request timed out after 3600000ms.INFO | jvm 1 | 2019/07/15 09:10:00 | at com.zaxxer.hikari.pool.HikariPool.createTimeoutException(HikariPool.java:548) ~[HikariCP-2.5.1.jar:na]INFO | jvm 1 | 2019/07/15 09:10:00 | at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:186) ~[HikariCP-2.5.1.jar:na]INFO | jvm 1 | 2019/07/15 09:10:00 | at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:145) ~[HikariCP-2.5.1.jar:na]INFO | jvm 1 | 2019/07/15 09:10:00 | at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:99) ~[HikariCP-2.5.1.jar:na]
2. Environments
- Spring Boot 1.x and 2.x
3. The Code
application.properties
spring.datasource.jdbcUrl=jdbc:mysql://10.1.1.8/testspring.datasource.username=bswenspring.datasource.driverClassName=com.mysql.jdbc.Driverspring.datasource.maximumPoolSize=20spring.datasource.connectionTimeout=3600000spring.datasource.idleTimeout=600000spring.datasource.minimumIdle=5spring.datasource.test-on-borrow=truespring.datasource.test-while-idle=truespring.datasource.time-between-eviction-runs-millis=3600000
Here, we configure a HikariCP connection pool with a connection size ranging from 5 to 20.
Spring Boot Command
@Componentpublic class MyCityCommand2 implements CommandLineRunner { private static Logger logger = LoggerFactory.getLogger(MyCityCommand2.class); @Autowired @Qualifier("cityDao") private CityDao cityDao;
@Override public void run(String... strings) throws Exception { while(true) { Thread.sleep(2000);
JdbcTemplate jdbcTemplate = cityDao.getJdbcTemplateObject(); // Get a JdbcTemplate try { // Get metadata from the connection String dbName = jdbcTemplate.getDataSource().getConnection().getMetaData().getDatabaseProductName(); logger.info("dbName:" + dbName); } catch (Exception ex) { logger.error("", ex); } finally { // Close the connection jdbcTemplate.getDataSource().getConnection().close(); } } }}
Pay attention to the lines:
jdbcTemplate.getDataSource().getConnection().getMetaData().getDatabaseProductName()
jdbcTemplate.getDataSource().getConnection().close()
In this code, we retrieve a connection from the JdbcTemplate
’s data source and then close it. However, this can lead to connection leaks because the connection retrieved and the one closed might not be the same.
When running the code, you might encounter the following error:
INFO | jvm 1 | 2019/07/15 09:10:00 | java.sql.SQLTransientConnectionException: HikariPool-2 - Connection is not available, request timed out after 3600000ms.
4. The Solution
Get and Close Specific Connection
To resolve this issue, ensure that you are closing the same connection you retrieved. Modify the code as follows:
@Overridepublic void run(String... strings) throws Exception { while(true) { Thread.sleep(2000);
JdbcTemplate jdbcTemplate = cityDao.getJdbcTemplateObject(); try (Connection conn = jdbcTemplate.getDataSource().getConnection()) { String dbName = conn.getMetaData().getDatabaseProductName(); logger.info("dbName:" + dbName); } }}
5. Summary
When using a connection pool in Spring Boot applications, it is crucial to manage connections properly to avoid leaks. If you encounter connection timeout issues, check whether you are closing the correct connection. Using try-with-resources can help ensure that connections are closed properly.
Final Words + More Resources
My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!