retry a connection to the database dropwizard

This will eventually cause a stack overflow. Better:

while (true) {
    try (
        new Application().run(args);
        return;
    } catch (SQLException e) {
        continue;
    }
}

But that’s not all: This will retry on any SQL exception. So, if, say, the db is up but the first thing the app does is, say, create a table, but that table already exists, this will just end up re-trying and failing, forever, at a 2 second interval, and that’s quite yucky. I suggest checking which -actual- SQLException is thrown if the DB is down (presumably, that’s why you want to retry: Wait for the DB to return) and catching ONLY that one. Note ethat SQLException has more methods than most exception types do, such as .getSQLState(), which you should check to figure out if the error is in fact ‘cannot connect to DB engine’, vs some other problem.

Furthermore, it is likely that Application’s run() method is catching all exceptions, and will log them, which means your attempt to catch them isn’t going to do anything. In this case, you’d have to edit the code of run().

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top