How to fix liquibase.exception.LockException: Could not acquire change log lock
This tutorial talks about Liquibase Lock exception during server startup.
Waiting for changelog lock.... Waiting for changelog lock.... Waiting for changelog lock.... Waiting for changelog lock.... Waiting for changelog lock.... Waiting for changelog lock.... Waiting for changelog lock.... Waiting for changelog lock.... Caused by: liquibase.exception.LockException: Could not acquire change log lock. Currently locked by Liquibase Update Failed: Could not acquire change log lock. Currently locked by S
This is the error when running database migration scripts or batch scripts and database is locked for updated.
First, Let’s understand the Liquibase Lock mechanism
How does Liquibase lock work?
Liquibase provides Lock services to maintain concurrency during multiple threads trying to update/modify the database. It allows to run a single thread always modify the database at a time using lokc mechanism.
Liquibase saves the changelog scripts changes in
DATABASECHANGELOCK table by default.
Applications configured to run a database migration scripts that runs during startup.
Liquibase does following things during startup,
- First, It acquires the lock and lock the database, i.e It creates
databasechangeloglocktable and adds an record to it. Once Lock is acquired, Only one thread can modify the database state.
databasechangeloglock contains a
locked column value
- Next, It runs the scripts configured in changeLogs
- Once the scripts are succesfull, It release the locks by updating databasechangeloglock table with row
Could not acquire change log lock issue occured if your application deployment is stopped once the lock is acquired.
The reason is Lock is still avialiable and not released.
Let’s see how to fix an error.
How to fix Liquibase Lock error
Database is still locked and need to release the lock to run the database scripts.
There are multiple ways to fix the issue
- First way, remove database lock tables
- First, Stop the application
- Connect to database, remove
databasechangelog.locktables in Database
TRUNCATE TABLE DATABASECHANGELOGLOCK or drop table DATABASECHANGELOGLOCK or delete from DATABASECHANGELOGLOCK
Restart the application
Second way, update the locked column value to zero
select * from DATABASECHANGELOGLOCK;
Outputs record that database is locked and
Locked > 0
Find the id and updated the record with given id by below command
update DATABASECHANGELOGLOCK set locked=0, lockgranted=null, lockedby=null where id=value
Locked=0 might change based on database, It can be