2017-06-12 71 views
0

我有一個連接到RDS(MySQL)的EC2實例的應用程序,8小時後DB連接從MySQL關閉並且當應用程序試圖讀取/寫入數據時以下例外Spring Boot和Spring雲AWS數據源池配置

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
  • 春季啓動1.5.4.RELEASE(JAR部署)
  • 春AWS雲和JDBC AWS autoconfigure,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> 

我的問題是:

我如何可以配置AWS(亞馬遜)在春季啓動數據源池?我登錄的數據源配置,一旦應用被部署在EC2上,它沒有配置與test-while-idle等配置,這裏是從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,但不能找到一種方法,從配置的池屬性文件(在我的情況YAML)...

回答

0

我發現下面的解決方法,但它會更好,通過相同的彈簧AWS-JDBC自動配置性能,以支持它。我添加了以下(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; 
     } 
    }; 
} 
}