Spring Boot communications link failure with MySQL and Hibernate

When you put a Spring Boot web application in production can happen that it will be inactive for several hours (e.g. for a whole night) without making any communication with database.

Using MySQL this can lead to a “communications link failure” error like this:

2015-07-08 09:16:32.666  WARN 20582 --- [http-nio-8080-exec-6] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 08S01
2015-07-08 09:16:32.668 ERROR 20582 --- [http-nio-8080-exec-6] o.h.engine.jdbc.spi.SqlExceptionHelper   : Communications link failure

The last packet successfully received from the server was 29.792.613 milliseconds ago.  The last packet sent successfully to the server was 6 milliseconds ago.

MySQL has a wait timeout limit set for default to 8 hours (28800 seconds). If the database connection is inactive for more than 8 hours it is automatically closed and the error above will happen.

How to avoid this

In Spring Boot, we can solve this problem adding these configurations in the application.properties file:

spring.datasource.testWhileIdle = true
spring.datasource.timeBetweenEvictionRunsMillis = 60000
spring.datasource.validationQuery = SELECT 1

Explanation: each hour (3600000 millis) minute (60000 millis) will be performed a connection test executing the query “SELECT 1”. In this way we can keep alive the database connection, periodically performing a validation query, and avoid to reach the MySQL’s wait_timeout.



MySQL wait_timeout and interactive_timeout

http://dev.mysql.com/doc/refman/5.5/en/server-system-variables.html#sysvar_wait_timeout http://www.rackspace.com/knowledge_center/article/how-to-change-the-mysql-timeout-on-a-server

  • Kevin


    • You’re welcome, Kevin.

  • Andrey Shchurkov

    Hey Andrea, thank you for the article. I wonder how does it intersect with maxIdle/minIdle settings. For example, if I have minIdle=10 and maxIdle=100, will I have always 100 connections, or will these settings let the number of connections drop to 10 and keep those 10 connections always up?

  • Hawk

    In spring boot 2 it uses hikari as defular connection pool provider. and spring boot 1.4+ defines specific namespaces for the four connections pools Spring Boot supports: tomcat, hikari, dbcp, dbcp2 (dbcp is deprecated as of 1.5). Now, Hikari doesn’t support the property you mentioned above,

    spring.datasource.testWhileIdle = true
    spring.datasource.timeBetweenEvictionRunsMillis = 60000
    spring.datasource.validationQuery = SELECT 1

    These properties are not supported in hikari. In this case what can be possible solution.


Category BootstrapCategory CoffeescriptCategory DrupalCategory GravCategory HTMLCategory JavascriptCategory JoomlaCategory jQueryCategory LaravelCategory MagentoCategory PHPCategory SharePointCategory SpringCategory ThymeleafCategory WordPressCategory Workflow


Developed and designed by Netgloo
© 2019 Netgloo