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:

src/main/resources/application.properties
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.

References

https://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html
http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html#boot-features-connect-to-production-database
http://stackoverflow.com/questions/3888776/basicdatasource-connection-time-out-problem-using-mysql
http://stackoverflow.com/questions/30451470/connection-to-db-dies-after-424-in-spring-boot-jpa-hibernate

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
http://serverfault.com/questions/355750/mysql-lowering-wait-timeout-value-to-lower-number-of-open-connections

  • Kevin

    thankx

    • 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.
    Thanks.

Categories

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

Comments

Developed and designed by Netgloo
© 2019 Netgloo