AWS Aurora Serverless Spring Boot Communication Link Error

﹥>﹥吖頭↗ 提交于 2021-02-11 17:01:51

问题


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:


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.



来源:https://stackoverflow.com/questions/63205733/aws-aurora-serverless-spring-boot-communication-link-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!