我在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: ******
我正在使用:
我的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配置,但未将其配置为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 :(得分: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;
}
};
}
}