By stoffer


2014-03-27 10:29:58 8 Comments

I have a nice little Spring Boot JPA web application. It is deployed on Amazon Beanstalk and uses an Amazon RDS for persisting data. It is however not used that often and therefore fails after a while with this kind of exception:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 79,870,633 milliseconds ago.
The last packet sent successfully to the server was 79,870,634 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.

I am not sure how to configure this setting and can not find information on it on http://spring.io (a very good site though). What are some ideas or pointers to information?

8 comments

@code4kix 2019-01-08 14:50:43

As some people already pointed out, spring-boot 1.4+, has specific namespaces for the four connections pools. By default, hikaricp is used in spring-boot 2+. So you will have to specify the SQL here. The default is SELECT 1. Here's what you would need for DB2 for example: spring.datasource.hikari.connection-test-query=SELECT current date FROM sysibm.sysdummy1

Caveat: If your driver supports JDBC4 we strongly recommend not setting this property. This is for "legacy" drivers that do not support the JDBC4 Connection.isValid() API. This is the query that will be executed just before a connection is given to you from the pool to validate that the connection to the database is still alive. Again, try running the pool without this property, HikariCP will log an error if your driver is not JDBC4 compliant to let you know. Default: none

@naoru 2018-02-08 15:45:28

whoami's answer is the correct one. Using the properties as suggested I was unable to get this to work (using Spring Boot 1.5.3.RELEASE)

I'm adding my answer since it's a complete configuration class so it might help someone using Spring Boot:

@Configuration
@Log4j
public class SwatDataBaseConfig {

    @Value("${swat.decrypt.location}")
    private String fileLocation;

    @Value("${swat.datasource.url}")
    private String dbURL;

    @Value("${swat.datasource.driver-class-name}")
    private String driverName;

    @Value("${swat.datasource.username}")
    private String userName;

    @Value("${swat.datasource.password}")
    private String hashedPassword;

    @Bean
    public DataSource primaryDataSource() {
        PoolProperties poolProperties = new PoolProperties();
        poolProperties.setUrl(dbURL);
        poolProperties.setUsername(userName);
        poolProperties.setPassword(password);
        poolProperties.setDriverClassName(driverName);
        poolProperties.setTestOnBorrow(true);
        poolProperties.setValidationQuery("SELECT 1");
        poolProperties.setValidationInterval(0);
        DataSource ds = new org.apache.tomcat.jdbc.pool.DataSource(poolProperties);
        return ds;
    }
}

@Uncle Long Hair 2019-03-01 17:39:22

Do you know why this custom code is needed and why Spring won't just read these properties from the properties file? I have several datasource properties in my file and it reads all the rest of them without a problem.

@Justin 2018-03-29 14:50:24

In case anyone is using custom DataSource

@Bean(name = "managementDataSource")
@ConfigurationProperties(prefix = "management.datasource")
public DataSource dataSource() {
    return DataSourceBuilder.create().build();
}

Properties should look like the following. Notice the @ConfigurationProperties with prefix. The prefix is everything before the actual property name

management.datasource.test-on-borrow=true
management.datasource.validation-query=SELECT 1

A reference for Spring Version 1.4.4.RELEASE

@whoami 2018-01-24 01:17:29

Setting spring.datasource.tomcat.testOnBorrow=true in application.properties didn't work.

Programmatically setting like below worked without any issues.

import org.apache.tomcat.jdbc.pool.DataSource;
import org.apache.tomcat.jdbc.pool.PoolProperties;    

@Bean
public DataSource dataSource() {
    PoolProperties poolProperties = new PoolProperties();
    poolProperties.setUrl(this.properties.getDatabase().getUrl());         
    poolProperties.setUsername(this.properties.getDatabase().getUsername());            
    poolProperties.setPassword(this.properties.getDatabase().getPassword());

    //here it is
    poolProperties.setTestOnBorrow(true);
    poolProperties.setValidationQuery("SELECT 1");

    return new DataSource(poolProperties);
}

@Justin 2018-03-29 14:51:09

If you are declaring a custom datasource it might be because you are trying to use the spring default .tomcat. So if you create a custom Datasource bean then add the @ConfigurationProperties(prefix = "spring.datasource.tomcat") to the DataSource bean and then it should allow you to set them in the application properties. My Example.. @Bean(name = "managementDataSource") @ConfigurationProperties(prefix = "management.datasource") public DataSource dataSource() { return DataSourceBuilder.create().build(); } management.datasource.test-on-borrow=true

@Jose Jurado 2016-09-29 09:35:53

I just moved to Spring Boot 1.4 and found these properties were renamed:

spring.datasource.dbcp.test-while-idle=true
spring.datasource.dbcp.time-between-eviction-runs-millis=3600000
spring.datasource.dbcp.validation-query=SELECT 1

@Stephen Harrison 2016-09-30 19:43:46

The names are equivalent. See the section on property naming in the Spring Boot docs.

@Y.M. 2016-11-29 14:01:47

@StephenHarrison : notice the dbcp.* prefix added in 1.4, relaxed binding doesn't apply in this case.

@Y.M. 2016-11-29 14:11:48

@Pawel : depending on which pooling implementation is available in your project, it might not be the dbcp.* properties for you, see Spring boot with SQL and the corresponding Datasource properties

@Stephane Nicoll 2014-03-27 12:18:27

I assume that boot is configuring the DataSource for you. In this case, and since you are using MySQL, you can add the following to your application.properties up to 1.3

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

As djxak noted in the comment, 1.4+ defines specific namespaces for the four connections pools Spring Boot supports: tomcat, hikari, dbcp, dbcp2 (dbcp is deprecated as of 1.5). You need to check which connection pool you are using and check if that feature is supported. The example above was for tomcat so you'd have to write it as follows in 1.4+:

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

Note that the use of autoReconnect is not recommended:

The use of this feature is not recommended, because it has side effects related to session state and data consistency when applications don't handle SQLExceptions properly, and is only designed to be used when you are unable to configure your application to handle SQLExceptions resulting from dead and stale connections properly.

@Stephane Nicoll 2015-02-03 11:36:09

that's because we've harmonized the way we write keys in documentation. We always used a relaxed binder so both spring.datasource.testOnBorrow and spring.datasource.test-on-borrow will work just fine. Check the documentation for more details.

@Rick 2015-06-23 02:53:55

Since it may confuse others: SELECT 1 guarantees that the connection has been tested before it's handed to the application. By using testOnBorrow = true, the objects will be validated before being borrowed from the pool. If the object fails to validate, it will be dropped from the pool, and will attempt to borrow another. NOTE – for a true value to have any effect, the validationQuery parameter must be set to a non-null string.

@Ruslan Stelmachenko 2017-01-18 13:29:05

Warning! In Spring Boot 1.4+ this was changed: there was defined new specific namespaces for the four connections pools spring supports: tomcat, hikari, dbcp, dbcp2. So, for example, for tomcat-jdbc connection-pool, the properties should be: spring.datasource.tomcat.testOnBorrow=true and spring.datasource.tomcat.validationQuery=SELECT 1.

@Nitish Kumar 2017-12-27 10:00:59

If I am myself configuring two different datasource then how do I provide these configuration? Do I need to provide this configuration for both the datasource like spring.datasource.mydatasource1.tomcat.testOnBorrow=true spring.datasource.mydatasource1.tomcat.validationQuery=SELEC‌​T 1 spring.datasource.mydatasource2.tomcat.testOnBorrow=true spring.datasource.mydatasource2.tomcat.validationQuery=SELEC‌​T 1 Or there is something else to follow??

@Chloe 2018-10-20 02:09:37

Warning! If you define any DataSource @Bean in your app, then Spring Boot won't configure the pool. docs.spring.io/spring-boot/docs/1.5.16.RELEASE/reference/… If you define your own DataSource bean, auto-configuration will not occur. I followed a guide for OAuth2 and had @Bean(name = "OAuth") public DataSource secondaryDataSource()... and it was not auto-configured nor using testOnBorrow.

@Stephane Nicoll 2018-11-05 15:14:34

Thanks Chloe but that's what happening for pretty much everything. If you define your own component, Spring Boot will detect it and back off.

@Soumya 2016-01-11 14:40:43

The above suggestions did not work for me. What really worked was the inclusion of the following lines in the application.properties

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

You can find the explanation out here

@Akeshwar Jha 2017-09-15 07:12:31

The link you've added says If the database connection is inactive for more than 8 hours it is automatically closed and the error above will happen. So, your solution is not to let the connection remain inactive for longer-durations. Is there a way I can connect to the SQL server after it has been restarted?

@grep 2015-09-07 11:24:34

I have similar problem. Spring 4 and Tomcat 8. I solve the problem with Spring configuration

<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
    <property name="initialSize" value="10" />
    <property name="maxActive" value="25" />
    <property name="maxIdle" value="20" />
    <property name="minIdle" value="10" />
     ...
    <property name="testOnBorrow" value="true" />
    <property name="validationQuery" value="SELECT 1" />
 </bean>

I have tested. It works well! This two line does everything in order to reconnect to database:

<property name="testOnBorrow" value="true" />
<property name="validationQuery" value="SELECT 1" />

Related Questions

Sponsored Content

27 Answered Questions

47 Answered Questions

[SOLVED] How to configure port for a Spring Boot application

1 Answered Questions

Could not open JPA Entity Manager

1 Answered Questions

[SOLVED] Spring Boot BrokenPipe / Communications Exception

0 Answered Questions

set autoReconnect=true, can't work in spring boot

  • 2017-02-27 07:06:59
  • Carvendy Xu
  • 615 View
  • 0 Score
  • 0 Answer
  • Tags:   spring-boot

1 Answered Questions

JDBC Connection: Not Recconecting after Timeout

1 Answered Questions

[SOLVED] CommunicationsException in webservice on Idle AWS instance

Sponsored Content