1

I have an application on EC2 instance that connect to RDS (MySQL), after 8 hours the DB connection gets closed from MySQL and when the application tries to read/write data I get the below Exception

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; 
nested exception is org.springframework.dao.DataAccessResourceFailureException: could not extract ResultSet;
nested exception is org.hibernate.exception.JDBCConnectionException: could not extract ResultSet] with root cause
java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.

also this Exception:

Request processing failed; nested exception is
org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction;
nested exception is javax.persistence.PersistenceException: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionEx
ception: No operations allowed after connection closed.] with root cause

java.net.SocketException: Connection timed out (Write failed)

this happens after 8 hours when the application is running. my config file (YAML):

management:
  security:
    enabled: false
spring:
  profiles: prod
  datasource:
    tomcat:
      max-active: 20
      max-idle: 10
      min-idle: 5
      initial-size: 5
      test-while-idle: true
      test-on-borrow: true
      test-on-return: true
      validation-query: select 2 from dual
      validation-interval: 3600
      time-between-eviction-runs-millis: 5000
  jpa:
    database: MYSQL
    generate-ddl: false
    show-sql: true
    properties:
      globally_quoted_identifiers: true 
    hibernate:
      ddl-auto: none
cloud:
  aws:
    stack:
      auto: false
    region:
      static: *****
    credentials:
      instanceProfile: true
    rds:
      dev-db:
        databaseName: dev-db
        username: ******
        password: ******

I'm using:

  • Java 1.8
  • Spring boot 1.5.4.RELEASE (JAR deployment)
  • Spring Cloud AWS JDBC and AWS autoconfigure, 1.1.3.RELEASE

My POM is:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-aws-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-aws-autoconfigure</artifactId>
        </dependency>

    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Camden.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <repositories>
        <repository>
            <id>io.spring.repo.maven.release</id>
            <url>http://repo.spring.io/release/</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

My Question is:

How can I configure Data source pool in Spring Boot for AWS (Amazon)? I Logged the DataSource configuration once the App is deployed on EC2, and it not configured to with test-while-idle and other configurations, here is the logs from EC2:

Data source Class Impl: class org.apache.tomcat.jdbc.pool.DataSource
 TimeBetweenEvictionRunsMillis: 5000
 ValidationInterval: 3000
 isTestOnBorrow: false
 isTestOnBorrow: false
 isTestOnBorrow: false

I checked this Page but cannot find a way to configure the pool from properties file (yaml in my case)...

Youssef Al Muhaidib
  • 363
  • 1
  • 5
  • 17

1 Answers1

0

I found below workaround, but it would be better to support it through same spring AWS-JDBC auto-config properties.. I added the below (from spring cloud aws)

@Configuration
@EnableRdsInstance(dbInstanceIdentifier = "test",password = "secret")
public class ApplicationConfiguration {

@Bean
public RdsInstanceConfigurer instanceConfigurer() {
    return new RdsInstanceConfigurer() {
        @Override
        public DataSourceFactory getDataSourceFactory() {
            TomcatJdbcDataSourceFactory dataSourceFactory = new TomcatJdbcDataSourceFactory();
            dataSourceFactory.setInitialSize(10);
            dataSourceFactory.setValidationQuery("SELECT 1 FROM DUAL");
            dataSourceFactory.setValidationInterval(10000);
            dataSourceFactory.setTimeBetweenEvictionRunsMillis(20000);
            return dataSourceFactory;
        }
    };
}
}
Youssef Al Muhaidib
  • 363
  • 1
  • 5
  • 17