我的Spring Boot应用程序使用Flyway进行db迁移,并连接到GCP Cloud中的Postgres。
它可以在JRE 11上工作,但在我们升级到JRE 17后开始中断。
日志显示服务启动成功并启动了Hikari连接,但是在Flyway试图获取连接时中断。
开头是这样的:
20:57:19.727 [main] INFO com.zaxxer.hikari.HikariDataSource [] - HikariPool-1 - Starting...
20:57:19.920 [main] INFO c.g.cloud.sql.core.CoreSocketFactory [] - First Cloud SQL connection, generating RSA key pair.
20:57:27.227 [main] INFO com.zaxxer.hikari.HikariDataSource [] - HikariPool-1 - Start completed.
20:57:27.420 [main] INFO o.f.c.i.d.base.BaseDatabaseType [] - Database: jdbc:postgresql://google/dbname_dev (PostgreSQL 13.7)
20:57:27.627 [main] INFO o.f.core.internal.command.DbValidate [] - Successfully validated 4 migrations (execution time 00:00.106s)
显示它获得了连接,验证了迁移,但随后显示错误:
WARN o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext [] - Exception
encountered during context initialization - cancelling refresh attempt:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'flywayInitializer' defined in class path resource
[org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]:
Unable to obtain connection from database: HikariPool-1 - Connection is not available,
request timed out after 30000ms.
HikariPool-1 - Connection is not available, request timed out after 30000ms.
最后:
20:57:57.653 [main] INFO com.zaxxer.hikari.HikariDataSource [] - HikariPool-1 - Shutdown initiated...
20:57:57.656 [main] INFO com.zaxxer.hikari.HikariDataSource [] - HikariPool-1 - Shutdown completed.
这个问题是由Flyway库引起的,因为一旦我禁用了spring.flyway.enabled=false
,应用程序就会按预期工作,并成功地与DB一起工作。
下面是POM片段:
JRE版本:
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>9.7.0</version>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-starter-sql-postgresql</artifactId>
<version>1.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>com.google.cloud.sql</groupId>
<artifactId>postgres-socket-factory</artifactId>
<version>1.4.4</version>
</dependency>
JRE 17 version:
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>9.16.1</version>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>9.16.1</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>spring-cloud-gcp-starter-sql-postgresql</artifactId>
<version>4.1.3</version>
</dependency>
<dependency>
<groupId>com.google.cloud.sql</groupId>
<artifactId>postgres-socket-factory</artifactId>
<version>1.11.0</version>
</dependency>
但我试着玩版本,我也试着保留旧版本,它仍然坏。
谢谢
我有同样的问题-对我来说,问题是我的Hikari池配置-我有
maximumPoolSize: 1
但是看起来从Flyway版本9.x开始。X -它一次需要多个连接。将其更改为10为我修复了一个错误。
maximumPoolSize: 10