Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flyway throws exception "HikariPool-1 Connection is not available" when trying to connect to GCP SQL

My Spring Boot application is using Flyway for db migrations and connects to Postgres in GCP Cloud.

It was working on JRE 11, but started to break after we upgraded to JRE 17.

The logs show that the service starts successfully and starts Hikari connection, but then it breaks on Flyway trying to obtain the connection.

So it starts like this:

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)

Shows that it got the connection, validated the migrations, but then shows error:

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.

and finally:

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.

The issue is caused by Flyway library, because once I disable using spring.flyway.enabled=false the application works as expected and successfully works with the DB.

Here are the POM snippets:

JRE 11 version:

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

But I tried to play with the versions, and I tried to keep the old versions too, it was still breaking.

Thank you

like image 388
yuriscom Avatar asked Nov 14 '25 22:11

yuriscom


2 Answers

I had same problem - for me the problem was my Hikari pool configuration - I had

maximumPoolSize: 1

But it looks like starting with Flyway version 9.x.x - it requires more than one connection at a time. Changing it to 10 fixed an error for me.

maximumPoolSize: 10
like image 177
Vladislav Varslavans Avatar answered Nov 19 '25 23:11

Vladislav Varslavans


I faced the same while upgrading from Spring Boot 2.7 to Spring Boot 3.1, increasing the connection pool did solve it. but using 2 connection pool should be enough.

the root casue for that depends on your database and the corresponding implementation of Database class of flywaydb.

flyway acquire either a single connection or 2 connections(migrations & history schema management) based on Database.useSingleConnection() evaluation.

In my case it was Postgres and in the past, it was evaluated as true so a single connection was used for both, now it evaluated as false in my case. github-flyway-single-connection-decistion

like image 40
Hanan Avatar answered Nov 19 '25 22:11

Hanan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!