【问题标题】:AWS Aurora Serverless Spring Boot Communication Link ErrorAWS Aurora Serverless Spring Boot 通信链路错误
【发布时间】: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


    【解决方案1】:

    在无服务器模式下,虽然高度优化并带有随时可用的实例的警告池,但您的应用程序需要一些时间才能真正连接到 Aurora。

    因此,很可能在应用程序启动时与数据库的连接尚不可用。

    对于您的错误跟踪,您似乎正在尝试执行某种 DDL 操作,可能是因为您正在使用 hbm2ddl 创建或验证数据库架构,并且此操作需要在应用程序启动时进行连接。

    为了支持这种数据库架构,您可以延迟初始化 bean(在实体管理器和 Hibernate 的情况下,这可能是基石),或者寻找像 Liquibase 这样的 DDL 生成替代方法(尽管我认为问题仍然存在)。

    您可以尝试调整 Hikari 连接参数。例如,本文提出了一个巧妙的配置,并提供了对 AWS Aurora 的深入了解:

    https://silexdata.com/modern-applications-and-aws-aurora-serverless/

    我唯一不明白的是为什么您的代码可以与 SimpleDriverDataSource 一起使用:也许它提供了一些默认值,允许您的应用从一开始就无错误地连接到 Aurora。

    【讨论】:

      猜你喜欢
      • 2020-01-31
      • 2021-02-05
      • 1970-01-01
      • 2019-11-30
      • 2019-07-31
      • 2020-03-19
      • 2020-01-28
      • 2020-12-17
      • 2023-01-12
      相关资源
      最近更新 更多