use of liquibase.exception.LockException in project liquibase by liquibase.
the class LockServiceExecuteTest method fixupLockTables.
private void fixupLockTables() throws DatabaseException, LockException {
for (Database database : TestContext.getInstance().getAllDatabases()) {
if (database.getConnection() != null) {
Statement statement = null;
try {
statement = ((JdbcConnection) database.getConnection()).getUnderlyingConnection().createStatement();
try {
statement.execute("drop table " + database.escapeTableName(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogTableName()));
} catch (Exception e) {
//ok
}
try {
statement.execute("drop table " + database.escapeTableName(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogLockTableName()));
} catch (Exception e) {
//ok
}
statement.close();
database.commit();
} catch (SQLException e) {
throw new DatabaseException(e);
}
}
}
}
use of liquibase.exception.LockException in project liquibase by liquibase.
the class StandardLockService method waitForLock.
@Override
public void waitForLock() throws LockException {
boolean locked = false;
long timeToGiveUp = new Date().getTime() + (getChangeLogLockWaitTime() * 1000 * 60);
while (!locked && new Date().getTime() < timeToGiveUp) {
locked = acquireLock();
if (!locked) {
LogFactory.getLogger().info("Waiting for changelog lock....");
try {
Thread.sleep(getChangeLogLockRecheckTime() * 1000);
} catch (InterruptedException e) {
;
}
}
}
if (!locked) {
DatabaseChangeLogLock[] locks = listLocks();
String lockedBy;
if (locks.length > 0) {
DatabaseChangeLogLock lock = locks[0];
lockedBy = lock.getLockedBy() + " since " + DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(lock.getLockGranted());
} else {
lockedBy = "UNKNOWN";
}
throw new LockException("Could not acquire change log lock. Currently locked by " + lockedBy);
}
}
use of liquibase.exception.LockException in project liquibase by liquibase.
the class StandardLockService method listLocks.
@Override
public DatabaseChangeLogLock[] listLocks() throws LockException {
try {
if (!this.hasDatabaseChangeLogLockTable()) {
return new DatabaseChangeLogLock[0];
}
List<DatabaseChangeLogLock> allLocks = new ArrayList<DatabaseChangeLogLock>();
SqlStatement sqlStatement = new SelectFromDatabaseChangeLogLockStatement("ID", "LOCKED", "LOCKGRANTED", "LOCKEDBY");
List<Map<String, ?>> rows = ExecutorService.getInstance().getExecutor(database).queryForList(sqlStatement);
for (Map columnMap : rows) {
Object lockedValue = columnMap.get("LOCKED");
Boolean locked;
if (lockedValue instanceof Number) {
locked = ((Number) lockedValue).intValue() == 1;
} else {
locked = (Boolean) lockedValue;
}
if (locked != null && locked) {
allLocks.add(new DatabaseChangeLogLock(((Number) columnMap.get("ID")).intValue(), (Date) columnMap.get("LOCKGRANTED"), (String) columnMap.get("LOCKEDBY")));
}
}
return allLocks.toArray(new DatabaseChangeLogLock[allLocks.size()]);
} catch (Exception e) {
throw new LockException(e);
}
}
use of liquibase.exception.LockException in project liquibase by liquibase.
the class Liquibase method update.
public void update(String tag, Contexts contexts, LabelExpression labelExpression) throws LiquibaseException {
if (tag == null) {
update(contexts, labelExpression);
return;
}
changeLogParameters.setContexts(contexts);
changeLogParameters.setLabels(labelExpression);
LockService lockService = LockServiceFactory.getInstance().getLockService(database);
lockService.waitForLock();
try {
DatabaseChangeLog changeLog = getDatabaseChangeLog();
checkLiquibaseTables(true, changeLog, contexts, labelExpression);
changeLog.validate(database, contexts, labelExpression);
List<RanChangeSet> ranChangeSetList = database.getRanChangeSetList();
ChangeLogIterator logIterator = new ChangeLogIterator(changeLog, new ShouldRunChangeSetFilter(database, ignoreClasspathPrefix), new ContextChangeSetFilter(contexts), new LabelChangeSetFilter(labelExpression), new DbmsChangeSetFilter(database), new UpToTagChangeSetFilter(tag, ranChangeSetList));
logIterator.run(createUpdateVisitor(), new RuntimeEnvironment(database, contexts, labelExpression));
} finally {
try {
lockService.releaseLock();
} catch (LockException e) {
log.severe("Could not release lock", e);
}
resetServices();
}
}
use of liquibase.exception.LockException in project liquibase by liquibase.
the class Liquibase method tagExists.
public boolean tagExists(String tagString) throws LiquibaseException {
LockService lockService = LockServiceFactory.getInstance().getLockService(database);
lockService.waitForLock();
try {
checkLiquibaseTables(false, null, new Contexts(), new LabelExpression());
return getDatabase().doesTagExist(tagString);
} finally {
try {
lockService.releaseLock();
} catch (LockException e) {
log.severe("Could not release lock", e);
}
}
}
Aggregations