Spring Boot JPA - configuring auto reconnect
Asked Answered
I

9

127

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?

Illusive answered 27/3, 2014 at 10:29 Comment(1)
Use this to print out your DataSource and verify its properties. https://mcmap.net/q/175927/-spring-boot-jdbc-hsqldb-how-to-verify-if-spring-boot-is-using-a-connection-pool Spring Boot will not auto-configure the DataSource if you have any @Beans which define a DataSource. docs.spring.io/spring-boot/docs/1.5.16.RELEASE/reference/…Fungi
G
158

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.

Gigantean answered 27/3, 2014 at 12:18 Comment(8)
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.Gigantean
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.Endive
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.Lanciform
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=SELECT 1 spring.datasource.mydatasource2.tomcat.testOnBorrow=true spring.datasource.mydatasource2.tomcat.validationQuery=SELECT 1 Or there is something else to follow??Ashlynashman
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.Fungi
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.Gigantean
If you are using hikari, which is the default connection pool in Spring Boot 2, you do not need to test the connection before it is handed to you, as hikari does it. Quote from HikariCP github page: ...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.Pancho
What I have to use if I have Hikari with spring boot, as I am getting this error on the production server?Une
G
30

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

Gesso answered 11/1, 2016 at 14:40 Comment(2)
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?Cymatium
Is it possible to set 28,800,000-1 instead of 3,600,000 to avoid timeout, according to MySQL documentation timeout?Catalectic
S
10

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);
}
Speroni answered 24/1, 2018 at 1:17 Comment(1)
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=trueAct
P
8

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
Petula answered 29/9, 2016 at 9:35 Comment(3)
The names are equivalent. See the section on property naming in the Spring Boot docs.Mauricio
@StephenHarrison : notice the dbcp.* prefix added in 1.4, relaxed binding doesn't apply in this case.Cajole
@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 propertiesCajole
B
4

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;
    }
}
Bravar answered 8/2, 2018 at 15:45 Comment(1)
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.Showers
T
3

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" />
Tallage answered 7/9, 2015 at 11:24 Comment(0)
A
3

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

Act answered 29/3, 2018 at 14:50 Comment(0)
B
3

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

Bart answered 8/1, 2019 at 14:50 Comment(0)
H
0

For those who want to do it from YAML with multiple data sources, there is a great blog post about it: https://springframework.guru/how-to-configure-multiple-data-sources-in-a-spring-boot-application/

It basically says you both need to configure data source properties and datasource like this:

@Bean
@Primary
@ConfigurationProperties("app.datasource.member")
public DataSourceProperties memberDataSourceProperties() {
    return new DataSourceProperties();
}

@Bean
@Primary
@ConfigurationProperties("app.datasource.member.hikari")
public DataSource memberDataSource() {
    return memberDataSourceProperties().initializeDataSourceBuilder()
            .type(HikariDataSource.class).build();
}

Do not forget to remove @Primary from other datasources.

Hanforrd answered 15/5, 2020 at 9:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.