AWS Aurora Serverless Spring Boot通信链路错误

huangapple go评论99阅读模式
英文:

AWS Aurora Serverless Spring Boot Communication Link Error

问题

我正在为将我们基于Spring Boot的应用程序迁移到AWS Aurora数据库,包括Serverless模式,制作一个原型。在预配模式下,一切都按预期工作。但是,在Serverless模式下,应用程序无法从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?这可能需要重新编写许多与数据库相关的代码。

英文:

I am doing a prototype for moving our Spring Boot based application to AWS Aurora DB including Serverless mode. With Provisioned mode things work as expected. However with serverless mode the application is not able to connect to the DB from EC2 instance with exception as:

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.

I have tried reducing the initial pool size etc but still get the same error

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

When I change the Datasource to SimpleDataSource or run a simple java jdbc connection from EC2 instance it works.

@Bean("dataSource")
public DataSource dataSource() {
  SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
  ....
}	  

Is this normal with a Spring boot JPA/JDBC Application? Do we have to use a SimpleDriverDataSource? Perhaps are we rather supposed to use https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/data-api.html? That will probably be re-writing a lot of DB related code.

答案1

得分: 2

在无服务器模式下,虽然已经高度优化并准备好可用实例的警告池,但在您实际连接到Aurora之前,您的应用程序需要一些时间。

因此,在应用程序启动时,与数据库的连接很可能还不可用。根据您的错误跟踪,似乎您正在尝试执行某种DDL操作,可能是因为您正在使用hbm2ddl来创建或验证数据库架构,而此操作仅在应用程序启动时需要连接。

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

您可以尝试调整Hikari连接参数。例如,这篇文章提供了一个聪明的配置,并提供了对AWS Aurora的深入了解:

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

我唯一不明白的是为什么您的代码能够与SimpleDriverDataSource一起运行:也许它提供了一些默认值,允许您的应用程序在一开始就连接到Aurora而没有错误。

英文:

In serverless mode, although highly optimized with a warn up pool of ready-to-go instances, your application requires some time until you can actually connect to Aurora.

For this reason, it is very likely that at the start of the application the connection to the database is not yet available.

It seems, for your error trace, that you are trying to do some kind of DDL operation, probably because you are using hbm2ddl to create or validate the database schema, and this operation requires a connection just while application is starting.

In order to support this kind of database architecture, you can lazily initialize your beans (which in the case of entity managers and Hibernate could be cornerstone), or look for an alternative approach for DDL generation like Liquibase (although I think the problem will remain).

You can try to tweak your Hikari connection parameters. For instance, this article propose a clever configuration and provides a great insight into AWS Aurora:

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

The only thing that I do not understand is why your code works with SimpleDriverDataSource: maybe it provides some defaults that allow your app to connect to Aurora without error from the beginning.

huangapple
  • 本文由 发表于 2020年8月1日 21:29:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63205733.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定