Exemplo n.º 1
0
    /**
     * Checks whether there's a local revision value in the database for this cluster node. If not,
     * it writes the given default revision to the database.
     *
     * @param revision the default value for the local revision counter
     * @return the local revision
     * @throws JournalException on error
     */
    protected synchronized long init(long revision) throws JournalException {
      ResultSet rs = null;
      try {
        // Check whether there is an entry in the database.
        rs = conHelper.exec(getLocalRevisionStmtSQL, new Object[] {getId()}, false, 0);
        boolean exists = rs.next();
        if (exists) {
          revision = rs.getLong(1);
        }

        // Insert the given revision in the database
        if (!exists) {
          conHelper.exec(insertLocalRevisionStmtSQL, revision, getId());
        }

        // Set the cached local revision and return
        localRevision = revision;
        initialized = true;
        return revision;

      } catch (SQLException e) {
        log.warn("Failed to initialize local revision.", e);
        throw new JournalException("Failed to initialize local revision", e);
      } finally {
        DbUtility.close(rs);
      }
    }
Exemplo n.º 2
0
  /**
   * {@inheritDoc}
   *
   * <p>This journal is locked by incrementing the current value in the table named <code>
   * GLOBAL_REVISION</code>, which effectively write-locks this table. The updated value is then
   * saved away and remembered in the appended record, because a save may entail multiple appends
   * (JCR-884).
   */
  protected void doLock() throws JournalException {
    ResultSet rs = null;
    boolean succeeded = false;

    try {
      startBatch();
    } catch (SQLException e) {
      throw new JournalException("Unable to set autocommit to false.", e);
    }

    try {
      conHelper.exec(updateGlobalStmtSQL);
      rs = conHelper.exec(selectGlobalStmtSQL, null, false, 0);
      if (!rs.next()) {
        throw new JournalException("No revision available.");
      }
      lockedRevision = rs.getLong(1);
      succeeded = true;
    } catch (SQLException e) {
      throw new JournalException("Unable to lock global revision table.", e);
    } finally {
      DbUtility.close(rs);
      if (!succeeded) {
        doUnlock(false);
      }
    }
  }
Exemplo n.º 3
0
    /** Cleans old revisions from the clustering table. */
    protected void cleanUpOldRevisions() {
      ResultSet rs = null;
      try {
        long minRevision = 0;
        rs = conHelper.exec(selectMinLocalRevisionStmtSQL, null, false, 0);
        boolean cleanUp = rs.next();
        if (cleanUp) {
          minRevision = rs.getLong(1);
        }

        // Clean up if necessary:
        if (cleanUp) {
          conHelper.exec(cleanRevisionStmtSQL, minRevision);
          log.info("Cleaned old revisions up to revision " + minRevision + ".");
        }

      } catch (Exception e) {
        log.warn("Failed to clean up old revisions.", e);
      } finally {
        DbUtility.close(rs);
      }
    }