question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow application to start even if connection fails

See original GitHub issue
Which version and edition of Flyway are you using?

7.4.0

If this is not the latest version, can you reproduce the issue with the latest one as well? (Many bugs are fixed in newer releases and upgrading will often resolve the issue)

Yes I can.

Which client are you using? (Command-line, Java API, Maven plugin, Gradle plugin)

Quarkus

Which database are you using? (Type & version)

Postgres 10

Which operating system are you using?

Linux, MacOS and Windows

What did you do? (Please include the content causing the issue, any relevant configuration settings, the SQL statement(s) that failed (if any), and the command you ran)

I’m writing a healthcheck to my app and I want it to go up even if Flyway fails to connect upon application start. In other words, I’m using the “5 minutes tutorial” to run an create an application, then I turned I pointed the DB to an invalid address and tried to start the application.

What did you expect to see?

I expected to have an option to tell flyway to give a warning or something that would tell me it wasn’t able to connect, instead of plain System.exit(1).

Something like:

// org.flywaydb.core.Flyway#execute (line ~542 at master)
// ... 
    this.failedState = false;
    // extracted it to a method to make it prettier
    result = doDatabaseExecution(all_the_necessary_parameters);
}

private <T> T createDatabase(all_the_necessary_parameters<T>) {
    Database database = null;
    try {
        // copy lines 544 through 580
    } catch (DataBaseConnectionException e) { // specific exception for said problem
        if (configuration.isFailOnDatabaseConnection) { // default = true
            throw e;
        }
        LOG.error("Unable to connect to database in order to run migrations. Moving to failed state.", e);
        this.failedState = true;
    } finally {
            IOUtils.close(database);
            showMemoryUsage();
    }
// ...

What did you see instead?

What I got was the default behaviour, which tries to connect and kills the application in case of errors.

I’ll post a pull request later on.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
tiobackcommented, Apr 15, 2021

As the Rubber Duck Theory comes into practice while I explain this issue to you, it just made me realize that it might not be a Flyway issue, after all. It seems like Quarkus is killing the application if the connection fails.

0reactions
tiobackcommented, Apr 15, 2021

Mikiel, I’ll go ahead and close this issue.

Despite Flyway being the one that threw the exception, Quarkus itself was the one responsible for terminating the application.

In the end, I had to switch Flyway to run manually inside a bean associated with Quarkus’ application lifecycle Startup phase.

Here’s a sample:

@Startup
@ApplicationScoped
public class MigrationService {

	private static final Logger LOGGER = LoggerFactory.getLogger(MigrationService.class);

	@Inject
	Flyway flyway;

        private boolean validState = false;

        @PostConstruct
	void checkMigration() {
		LOGGER.info("Checking migration.");
		try {
			// Use the flyway instance manually
			flyway.clean();
			flyway.migrate();
			LOGGER.info("[FlyWay] DB Current Version: " + flyway.info().current().getVersion().toString());
                         validState = true;
		} catch (Exception e) {
			LOGGER.error("Something went wrong.", e);
		}
	}

        public boolean hasValidState() {
            return this.validState;
        }
}

I also had to set quarkus.flyway.migrate-at-start=false at application.properties in order to prevent io.quarkus.flyway.FlywayProcessor from running the integration automatically before my managed bean.

It would be nice to extend FlywaySqlException into a FlywaySqlConnectionException in order to handle it in different ways, though.

Thanks for waiting.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to make Spring server to start even if database is down?
You can set: spring.sql.init.continue-on-error=true. in your application.properties. According to the Spring Boot 2.5.5 user guide:.
Read more >
Allow an app to start when its DataSource is unable to connect ...
I stumbled about this behaviour when I tried to start a Spring Boot application along with a database via Docker Compose. The Docker ......
Read more >
Executing Code on Spring Boot Application Startup
Spring Boot offers many different solutions to run code at application startup. This article explains how they work and when to use which....
Read more >
Top 10 Most Common Spring Framework Mistakes - Toptal
In this article we'll cover some of the more common mistakes in Spring, specifically oriented towards web applications and Spring Boot. As Spring...
Read more >
How to perform a clean boot in Windows - Microsoft Support
A "clean boot" starts Windows with a minimal set of drivers and startup programs, so that you can determine whether a background program...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found