1

我在 EC2 实例上有一个连接到 RDS(MySQL)的应用程序,8 小时后数据库连接从 MySQL 关闭,当应用程序尝试读/写数据时,我得到以下异常

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.

还有这个例外:

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)

这会在应用程序运行 8 小时后发生。我的配置文件(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: ******

我在用着:

  • Java 1.8
  • Spring boot 1.5.4.RELEASE(JAR部署)
  • Spring Cloud AWS JDBC 和 AWS 自动配置,1.1.3.RELEASE

我的POM是:

<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>

我的问题是:

如何在 Spring Boot for AWS (Amazon) 中配置数据源池?一旦应用程序部署在 EC2 上,我就记录了 DataSource 配置,但它没有配置为 withtest-while-idle和其他配置,这是来自 EC2 的日志:

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

我检查了此页面,但找不到从属性文件(在我的情况下为 yaml)配置池的方法...

4

1 回答 1

0

我在下面找到了解决方法,但最好通过相同的 spring AWS-JDBC 自动配置属性来支持它。我添加了以下内容(来自 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;
        }
    };
}
}
于 2017-06-16T23:05:34.070 回答