Skip to content

Commit

Permalink
Merge pull request #522 from liquibase/DAT-17365
Browse files Browse the repository at this point in the history
[DAT-17365] Release lock even if update fails on timeout
  • Loading branch information
vitaliimak committed Apr 12, 2024
2 parents 8360031 + d4281c8 commit c559f9a
Showing 1 changed file with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
* #L%
*/

import com.mongodb.MongoInterruptedException;
import liquibase.Scope;
import liquibase.database.Database;
import liquibase.exception.DatabaseException;
import liquibase.exception.LockException;
import liquibase.ext.mongodb.database.MongoLiquibaseDatabase;
import liquibase.ext.mongodb.statement.CountCollectionByNameStatement;
import liquibase.ext.mongodb.statement.DropCollectionStatement;
Expand Down Expand Up @@ -66,9 +68,26 @@ protected Boolean isLocked() throws DatabaseException {

@Override
protected int replaceLock(final boolean locked) throws DatabaseException {
return getExecutor().update(
new ReplaceChangeLogLockStatement(getDatabaseChangeLogLockTableName(), locked)
);
try {
return getExecutor().update(
new ReplaceChangeLogLockStatement(getDatabaseChangeLogLockTableName(), locked)
);
} catch (DatabaseException e) {
// Mongo driver does not allow to release lock if thread is interrupted
// Next code clears interrupted flag if it is happened due to of the timeout
// Tries to release lock again
// Restore thread interrupt status
if (e.getCause() instanceof MongoInterruptedException && e.getCause().getSuppressed().length > 0
&& e.getCause().getSuppressed()[0].getMessage().equals("Interrupted waiting for lock") && Thread.interrupted()) {
try {
releaseLock();
} catch (LockException ex) {
throw new DatabaseException(ex);
}
Thread.currentThread().interrupt();
}
throw e;
}
}

@Override
Expand Down

0 comments on commit c559f9a

Please sign in to comment.