Skip to content

Commit

Permalink
[Draft] Impl with consideration of lock duration
Browse files Browse the repository at this point in the history
  • Loading branch information
VaishSiddharth committed May 9, 2024
1 parent e9f05b9 commit c8c26c7
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion app/src/main/java/com/ozonehis/eip/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import liquibase.database.Database;
import liquibase.database.DatabaseFactory;
import liquibase.database.jvm.JdbcConnection;
import liquibase.lockservice.LockService;
import liquibase.lockservice.LockServiceFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
Expand Down Expand Up @@ -40,7 +41,19 @@ private static void releaseLiquibaseLock() {
try (Connection connection = dataSource.getConnection()) {
Database database = DatabaseFactory.getInstance()
.findCorrectDatabaseImplementation(new JdbcConnection(connection));
LockServiceFactory.getInstance().getLockService(database).forceReleaseLock();
// LockServiceFactory.getInstance().getLockService(database).forceReleaseLock();
LockService lockService = LockServiceFactory.getInstance().getLockService(database);
if (lockService.hasChangeLogLock()) {
long lockAcquiredAt = lockService.listLocks()[lockService.listLocks().length - 1].getLockGranted().getTime();
long currentTime = System.currentTimeMillis();
long lockDuration = currentTime - lockAcquiredAt;
if (lockDuration > 20000) {
lockService.forceReleaseLock();
} else {
log.info("Liquibase lock is not held for more than 20 seconds. No action needed.");
}
}

} catch (Exception e) {
log.error("Error occurred while releasing lock from Liquibase: {}", e.getMessage(), e);
}
Expand Down

0 comments on commit c8c26c7

Please sign in to comment.