【发布时间】:2020-08-01 13:29:35
【问题描述】:
我正在制作一个原型,用于将我们基于 Spring Boot 的应用程序迁移到 AWS Aurora DB,包括无服务器模式。在预置模式下,事情按预期工作。但是,在无服务器模式下,应用程序无法从 EC2 实例连接到数据库,但以下情况除外:
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'entityManagerFactory' defined in class path resource
[org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]:
Invocation of init method failed; nested exception is javax.persistence.PersistenceException:
[PersistenceUnit: default] Unable to build Hibernate SessionFactory;
nested exception is org.hibernate.exception.JDBCConnectionException:
Unable to open JDBC Connection for DDL execution
.....
Caused by: com.mysql.cj.jdbc.exceptions.CommunicationsException:
Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago.
The driver has not received any packets from the server.
我已经尝试减少初始池大小等,但仍然遇到相同的错误
spring.datasource.url=jdbc:mysql://xxxxxx.cluster-xxxxxxxxxx.us-east-1.rds.amazonaws.com:3306/db_example
spring.datasource.username=xxxx
spring.datasource.password=xxxxxxx
spring.datasource.hikari.maximum-pool-size=1
spring.datasource.hikari.minimum-idle=0
spring.datasource.hikari.connection-timeout=60000
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
....
当我将数据源更改为 SimpleDataSource 或从 EC2 实例运行简单的 java jdbc 连接时,它可以工作。
@Bean("dataSource")
public DataSource dataSource() {
SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
....
}
这对于 Spring Boot JPA/JDBC 应用程序是否正常?我们必须使用 SimpleDriverDataSource 吗?也许我们更应该使用https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/data-api.html?那可能会重写很多与数据库相关的代码。
【问题讨论】:
标签: java amazon-web-services spring-boot serverless amazon-aurora