Search code examples
javaspringlockingdatabase-migrationliquibase

Liquibase Double-Checked Locking Pattern ? (make liquibase not acquiring lock if everything is up-to-date)


If an application crashes (OOM / power failure / kill -9) after liquibase acquired a lock, but before releasing the lock, the lock will remain in the databasechangeloglock table forever and we'll need to manually remove it.

I wonder if it's possible to force the application with liquibase do this:

  1. check if it needs to apply any changesets WITHOUT taking a lock
  2. if so, acquire the lock, check the need again, and so on as usual. If everything is up to date - do not acquire lock and continue the boot strap.

I want it to speed up application bootstrap (when there's no new migrations) and reduce chances to get stuck lock in the databasechangeloglock.

Currently I just create a liquibase.integration.spring.SpringLiquibase Spring bean. I assume I need to create some custom tricky @ConditionalOn... annotation


Solution

  • Liquibase already does 1 using Fastcheck: here and here. That's something relatively new. But the accepted answer is also good!