/** * This method currently checks the liquibasechangeloglock table to see if there is a row * with a lock in it. This uses the liquibase API to do this * * @return true if database is currently locked */ public static boolean isLocked() { Database database = null; try { Liquibase liquibase = getLiquibase(null, null); database = liquibase.getDatabase(); return LockService.getInstance(database).listLocks().length > 0; } catch (Exception e) { return false; } finally { try { database.getConnection().close(); } catch (Exception e) { // pass } } } }
private LockException attemptToAcquireLock() { try { super.waitForLock(); return null; } catch (LockException e) { return e; } }
@Override public int getPriority() { return super.getPriority() + 1; // Liquibase chooses which LockService to use based on its priority. This line makes sure that our // custom lock service has a higher priority than the standard one (which it extends). }
public void releaseLocks() throws Exception { LockService lockService = LockServiceFactory.getInstance().getLockService(database); lockService.forceReleaseLock(); System.out.println("successfully released the database"); }
database = liquibase.getDatabase(); if (database.hasDatabaseChangeLogLockTable() && isLocked()) { LockService.getInstance(database).forceReleaseLock();
/** * Interface used for callbacks when updating the database. Implement this interface and pass it * to {@link DatabaseUpdater#executeChangelog(String, Map, ChangeSetExecutorCallback)} */ public interface ChangeSetExecutorCallback { /** * This method is called after each changeset is executed. * * @param changeSet the liquibase changeset that was just run * @param numChangeSetsToRun the total number of changesets in the current file */ public void executing(ChangeSet changeSet, int numChangeSetsToRun); }
@Override public void contextInitialized(ServletContextEvent event) { LockService lockService = new RecoveringLockService(); LockServiceFactory.getInstance() .register(lockService); LOGGER.info(MessageFormat.format(Messages.REGISTERED_0_AS_LIQUIBASE_LOCK_SERVICE, lockService.getClass() .getName())); }
private boolean lockIsStuck() throws LockException { DatabaseChangeLogLock lock = getDatabaseChangeLogLock(); if (lock == null) { return false; } Date lockGranted = lock.getLockGranted(); Date currentDate = new Date(); LOGGER.info(format(Messages.CURRENT_LOCK, lockGranted, lock.getLockedBy())); LOGGER.info(format(Messages.CURRENT_DATE, currentDate)); return hasTimedOut(lockGranted, currentDate); }
private static void releaseLockOnTimeout(Liquibase liquibase) throws LiquibaseException, UnsupportedEncodingException { if (LOCK_RELEASE_TIMEOUT_MINUTES <= 0) { return; } for (DatabaseChangeLogLock lock : liquibase.listLocks()) { long elapsedMinutes = TimeUnit.MILLISECONDS .toMinutes(System.currentTimeMillis() - lock.getLockGranted().getTime()); if (elapsedMinutes >= LOCK_RELEASE_TIMEOUT_MINUTES) { ByteArrayOutputStream os = new ByteArrayOutputStream(); liquibase.reportLocks(new PrintStream(os)); logger.warning(String.format("Releasing liquibase locks after %d minutes: %s", elapsedMinutes, os.toString(Utils.CHARSET))); liquibase.forceReleaseLocks(); break; } } }