Search in sources :

Example 11 with LockException

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);
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Database(liquibase.database.Database) JdbcConnection(liquibase.database.jvm.JdbcConnection) DatabaseException(liquibase.exception.DatabaseException) LockException(liquibase.exception.LockException) DatabaseException(liquibase.exception.DatabaseException) SQLException(java.sql.SQLException)

Example 12 with LockException

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);
    }
}
Also used : LockException(liquibase.exception.LockException) Date(java.util.Date)

Example 13 with LockException

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);
    }
}
Also used : ArrayList(java.util.ArrayList) Date(java.util.Date) LockException(liquibase.exception.LockException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) InvalidExampleException(liquibase.snapshot.InvalidExampleException) DatabaseException(liquibase.exception.DatabaseException) LiquibaseException(liquibase.exception.LiquibaseException) SqlStatement(liquibase.statement.SqlStatement) LockException(liquibase.exception.LockException) Map(java.util.Map)

Example 14 with LockException

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();
    }
}
Also used : LockService(liquibase.lockservice.LockService) LockException(liquibase.exception.LockException)

Example 15 with LockException

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);
        }
    }
}
Also used : LockService(liquibase.lockservice.LockService) LockException(liquibase.exception.LockException)

Aggregations

LockException (liquibase.exception.LockException)18 LockService (liquibase.lockservice.LockService)13 LiquibaseException (liquibase.exception.LiquibaseException)5 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)5 DatabaseException (liquibase.exception.DatabaseException)4 Executor (liquibase.executor.Executor)3 InvalidExampleException (liquibase.snapshot.InvalidExampleException)3 Date (java.util.Date)2 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)2 Sql (liquibase.sql.Sql)2 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 Database (liquibase.database.Database)1 ObjectQuotingStrategy (liquibase.database.ObjectQuotingStrategy)1 JdbcConnection (liquibase.database.jvm.JdbcConnection)1 LoggingExecutor (liquibase.executor.LoggingExecutor)1 SqlStatement (liquibase.statement.SqlStatement)1