48

I have an app that uses spring-boot,jpa-hiberanate with mysql.I am getting this error log

Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 56,006,037 milliseconds ago.  The last packet sent successfully to the server was 56,006,037 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.

Here is my application.properties

# DataSource settings: set here configurations for the database connection
spring.datasource.url = jdbc:mysql://localhost:3306/test
spring.datasource.username = test
spring.datasource.password = test
spring.datasource.driverClassName = com.mysql.jdbc.Driver

# Specify the DBMS
spring.jpa.database = MYSQL

# Show or not log for each sql query
spring.jpa.show-sql = true

# Hibernate settings are prefixed with spring.jpa.hibernate.*
spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.naming_strategy = org.hibernate.cfg.ImprovedNamingStrategy

To solve this issue I can use

spring.datasource.testOnBorrow=true
spring.datasource.validationQuery=SELECT 1

But I checked that it's not recommended .So can anyone suggest me what should I do to overcome this error

Soham
  • 4,397
  • 11
  • 43
  • 71
  • 1
    Take a look at the following post. http://hibernatedb.blogspot.in/2009/05/automatic-reconnect-from-hibernate-to.html , this was a comment link which was intriguing from http://stackoverflow.com/questions/2077081/connection-with-mysql-is-being-aborted-automaticly-how-to-configure-connector-j – Kenneth Clark May 26 '15 at 07:27
  • @KennethClark I am using spring-boot not spring so these wouldn't helped me much.And I don't know much about spring also. – Soham May 26 '15 at 07:34
  • Spring Boot is based on Spring, so if you use Spring Boot, of course you are also using Spring. You should read more about the basics of Spring Boot, if you don't know that. – dunni May 26 '15 at 08:05
  • 1
    Why wouldn't it be recommended to validate the connection? The `autoReconnect` property isn't recommended. – M. Deinum May 26 '15 at 08:11
  • @M.Deinum I have given the link above for the reason . It has side effects related to session state and data consistency when applications don't handle SQLExceptions properly. – Soham May 26 '15 at 08:28
  • @dunni ,yes i agree with you but I haven't found any solution for the above error,maybe I missed that.It would be better if you can guide me to the solution. – Soham May 26 '15 at 08:30
  • @Soham I strongly suggest a read again as that is for the `autoReconnect` property NOT for those 2 properties you are specifying... – M. Deinum May 26 '15 at 09:02
  • @M.Deinum sorry my mistake,it's written here http://stackoverflow.com/questions/22684807/spring-boot-jpa-configuring-auto-reconnect – Soham May 26 '15 at 09:29
  • No it is not, that is also about the `autoReconnect` property. – M. Deinum May 26 '15 at 09:30
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/78781/discussion-between-soham-and-m-deinum). – Soham May 26 '15 at 09:58

1 Answers1

113

The easiest way is to specify the autoReconnect property in the JDBC url, although this isn't the recommended approach.

spring.datasource.url = jdbc:mysql://localhost:3306/test?autoReconnect=true

This can give issues when you have an active connection and during a transaction something happens and a reconnect is going to happen. It will not give issues when the connection is validated at the start of the transaction and a new connection is acquired at the start.

However it is probably better to enable validation of your connections during the lifetime of your application. For this you can specify several properties.

First start by specifying maximum number of connections you allow for the pool. (For a read on determining the max poolsize read this).

spring.datasource.max-active=10

You also might want to specify the number of initial connections

spring.datasource.initial-size=5

Next you want to specify the min and max number of idle connections.

spring.datasource.max-idle=5
spring.datasource.min-idle=1

To validate connection you need to specify a validation-query and when to validate. As you want to validate periodically, instead of when a connection is retrieved from the pool (this to prevent broken connections in your pool).

spring.datasource.test-while-idle=true
spring.datasource.test-on-borrow=true
spring.datasource.validation-query=SELECT 1

NOTE: The usage of a validation-query is actually discouraged with as JDBC4 has a better/different way of doing connection validation. HikariCP will automatically call the JDBC validation method when available.

Now that you are also validating while a connection is idle you need to specify how often you want to run this query for the connections and when a connection is considered idle.

spring.datasource.time-between-eviction-runs-millis=5000 (this is the default)
spring.datasource.min-evictable-idle-time-millis=60000 (this is also default)

This all should trigger validation of your (idle) connections and when an exception occurs or the idle period has passed your connections will be removed from the pool.

Assuming you are using Tomcat JDBC as the connection pool this is a nice read of what and how to configure.

UPDATE: Spring Boot 2.x switched the default connection pool to HikariCP instead of Tomcat JDBC.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
  • 2
    Thanks. Is the `validation-query` run every `time-between-eviction-runs-millis` OR `min-evictable-idle-time-millis` ? – phanin Dec 23 '16 at 00:46
  • 4
    As of Spring Boot 1.4.1, [many of these properties are ignored](https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-1.4-Release-Notes#datasource-binding), instead you have to use `spring.datasource.tomcat` for the tomcat-specific properties. – ben3000 Aug 03 '17 at 03:04
  • 2
    See [Tomcat documentation](https://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html#Attributes) for a description of Tomcat specific properties (`spring.datasource.tomcat.*`) and their default values. – user909481 Aug 03 '17 at 11:45
  • does this post mean to have autoReconnect with url including all the other properties to set as mentioned or autoReconnect is not required when other properties are set? I've been using spring boot 2.3.3 which has hibernate 5.1.3. What are the minimum properties required that also boot performance solving this issue? – Sanjay Amin Apr 29 '21 at 22:46