Flyway, spring boot and application start without database

Viewed 1347

I'm trying to make Spring Boot application with Flyway (and Hikari pool) to start the server even when the DB is not available at that time.

I need to support cases when: 1. DB is not available when applicaition starts (it should run Flyway after DB starts, it can be up to 30 mins). 2. DB goes offline during the application lifetime and then goes back up.

I got a problem with the first case, Flyway always tries to do migrations even when DB is not available and application stops. I tried adding spring.datasource.continue-on-error: true but Flyway ignores that, and I couldn't find any flyway configuration that would allow such operation.

Is it possible or should I wrap Flyway and do it myself?

Spring boot 2.1.4

1 Answers

A couple of points to consider What is the desired behavior of the application when the DB is really not available when the instance of java application? Ok, so flyway won't start, but how the application will be able to handle requests that will have to reach the database? Flyway itself relies on DataSource bean, maybe on hibernate if you use it, and these are much more complicated infrastructures than flyway itself?

Maybe if the database is not available the application won't need to start at all? Instead it worth to rely on orchestrators (like kubernetes, ECS or whatever that will recognize that the application didn't start and will try to retrigger the start again, again, and again till the database will be ready)?

This is my recommendation in general.

Now, assuming find answers to all these questions and still, want to proceed with this path:

Spring Boot by itself works like this when it comes to flyway integration:

If the relevant classes (Flyway class) exist on classpath and spring.flyway.enabled=true then the bean of flyway starts and spring boot does its magic.

Technically the relevant auto configuration can be found in class org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration (org.springframework.boot:spring-boot-autoconfigure module)

I think the way to go is to disable flyway, and given that fact that beans like DataSource are available (somehow) - create a Flyway Bean by yourself and trigger the migration in some kind of loop in the background that will exit only if the migration actually succeeds (or already applied)

Related