Search code examples
javaspring-bootflyway

Spring boot 2: Apparent connection leak detected when using Flyway


I have spring boot 2 app, I am using Flyway for DB migrations. I programmatically execute flyway migration at the app start. When doing that I got: java.lang.Exception: Apparent connection leak detected

On the row:

Database database = org.flywaydb.core.internal.database.DatabaseFactory.createDatabase(flyway, false);

Here is my Hikari CP config:

spring.datasource.max-active=20
spring.datasource.idle-timeout=10000
spring.datasource.initial-size=20
spring.datasource.test-on-return=true
spring.datasource.hikari.leak-detection-threshold=10000

Solution

  • So the Flyway apparently does not close connections. The solution is to handle that manually:

    try (Database database = DatabaseFactory.createDatabase(flyway, false)) {
         database.getMainConnection(); // this is important, otherwise there will be connection leak
         doRunMigrations(database);
    }