Executing customChange migrations fails with "This connection has been closed.""

Using gradle, I do have the following pipeline:

  1. liquibase runs against local postgres to apply SQL-based changes
  2. JOOQ generator invoked right after it and generates the enteties
  3. another round of liquibase is started, but this time contains only code-based (aka customChange) migrations

On step 3 it is always failing with

Caused by: liquibase.exception.DatabaseException: Error executing SQL SHOW SEARCH_PATH: This connection has been closed.
        at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:89)
        at liquibase.executor.jvm.JdbcExecutor.query(JdbcExecutor.java:201)
        at liquibase.executor.jvm.JdbcExecutor.query(JdbcExecutor.java:209)
        at liquibase.executor.jvm.JdbcExecutor.queryForObject(JdbcExecutor.java:217)
        at liquibase.executor.jvm.JdbcExecutor.queryForObject(JdbcExecutor.java:232)
        at liquibase.executor.jvm.JdbcExecutor.queryForObject(JdbcExecutor.java:227)
        at liquibase.database.core.DatabaseUtils.initializeDatabase(DatabaseUtils.java:41)
        at liquibase.database.core.PostgresDatabase.rollback(PostgresDatabase.java:403)
        at liquibase.lockservice.StandardLockService.releaseLock(StandardLockService.java:361)

The customChange looks pretty standard:

class Migration1 : CustomTaskChange {

    private lateinit var resourceAccessor: ResourceAccessor

    override fun getConfirmationMessage(): String = "Migration ${this.javaClass.name} completed successfully."

    override fun setUp() {
        // noop
    }

    override fun setFileOpener(resourceAccessor: ResourceAccessor) {
        this.resourceAccessor = resourceAccessor
    }

    override fun validate(database: Database): ValidationErrors = ValidationErrors()

    override fun execute(database: Database) {
        try {
            val connection = database.connection as JdbcConnection
            connection.use {
                it.wrappedConnection.use { psql ->
                    DSL.using(psql).execute()
                }
            }
        } catch (e: Exception) {
            throw CustomChangeException(e)
        }
    }
}

I also tried the basic example of

override fun execute(database: Database) {
        try {
            val connection = database.connection as JdbcConnection
            val test = connection.createStatement().executeQuery("select * from workflow")
            test.next()
        } catch (e: Exception) {
            throw CustomChangeException(e)
        }
    }

which is also fails with the same error.

Any idea? Thank you in advance

I think I found the reason, will put it here just in case someone else faces it.
Basically the issue was the connection.use statement, somehow it causes the connection to be closed at time I try to use it. The following code runs without any issue:

override fun execute(database: Database) {
     try {
         val connection = database.connection as JdbcConnection
         DSL.using(connection.wrappedConnection).execute()
     } catch (e: Exception) {
         throw CustomChangeException(e)
     }
}
1 Like

Hi @vicmosin,

Glad you can figure it out. If you are ok with your solution could you please close this discussion. Or if you have any other question please let us know that either myself or someone from the team will try to assist you.

Thanks,
Daniel.