Example #1
0
  public DatabaseChangeLogLock[] listLocks() throws DatabaseException {
    try {
      if (!database.hasDatabaseChangeLogLockTable()) {
        return new DatabaseChangeLogLock[0];
      }

      List<DatabaseChangeLogLock> allLocks = new ArrayList<DatabaseChangeLogLock>();
      SqlStatement sqlStatement =
          new SelectFromDatabaseChangeLogLockStatement("ID", "LOCKED", "LOCKGRANTED", "LOCKEDBY");
      List<Map> 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()]);
    } finally {
      database
          .rollback(); // Rollback to make sure any database explicitly imposed lock from the above
                       // SELECT statement gets released. CORE-1219 incident.
    }
  }
Example #2
0
  public void releaseLock() throws DatabaseException, LockException {
    Executor executor = ExecutorService.getInstance().getExecutor(database);
    try {
      if (database.hasDatabaseChangeLogLockTable()) {
        executor.comment("Release Database Lock");
        database.rollback();
        int updatedRows = executor.update(new UnlockDatabaseChangeLogStatement());
        if (updatedRows != 1) {
          throw new LockException(
              "Did not update change log lock correctly.\n\n"
                  + updatedRows
                  + " rows were updated instead of the expected 1 row using executor "
                  + executor.getClass().getName()
                  + " there are "
                  + executor.queryForInt(
                      new RawSqlStatement(
                          "select count(*) from " + database.getDatabaseChangeLogLockTableName()))
                  + " rows in the table");
        }
        database.commit();
        hasChangeLogLock = false;

        instances.remove(this.database);

        database.setCanCacheLiquibaseTableInfo(false);

        LogFactory.getLogger().info("Successfully released change log lock");
      }
    } finally {
      database.rollback();
    }
  }