/**
   * Shuts down the logging process using the specified mode.
   *
   * <p>
   *
   * @param closemode The mode in which to shut down the logging process
   *     <OL>
   *       <LI>closemode -1 performs SHUTDOWN IMMEDIATELY, equivalent to a poweroff or crash.
   *       <LI>closemode 0 performs a normal SHUTDOWN that checkpoints the database normally.
   *       <LI>closemode 1 performs a shutdown compact that scripts out the contents of any CACHED
   *           tables to the log then deletes the existing *.data file that contains the data for
   *           all CACHED table before the normal checkpoint process which in turn creates a new,
   *           compact *.data file.
   *     </OL>
   *
   * @return true if closed with no problems or false if a problem was encountered.
   */
  boolean closeLog(int closemode) {

    if (lLog == null) {
      return true;
    }

    try {
      lLog.stop();

      switch (closemode) {
        case Database.CLOSEMODE_IMMEDIATELY:
          lLog.shutdown();
          break;

        case Database.CLOSEMODE_NORMAL:
          lLog.close(false, true);
          break;

        case Database.CLOSEMODE_COMPACT:
        case Database.CLOSEMODE_SCRIPT:
          lLog.close(true, true);
          break;
      }
    } catch (Throwable e) {
      lLog = null;

      return false;
    }

    lLog = null;

    return true;
  }
  /**
   * Method declaration
   *
   * @param compact
   * @throws SQLException
   */
  void close(boolean compact) throws SQLException {

    if (Trace.TRACE) {
      Trace.trace();
    }

    if (bReadOnly) {
      return;
    }

    // no more scripting
    closeScript();

    // create '.script.new' (for this the cache may be still required)
    writeScript(compact);

    // flush the cache (important: after writing the script)
    if (cCache != null) {
      cCache.flush();
    }

    closeAllTextCaches(compact);

    // create '.backup.new' using the '.data'
    backup();

    // we have the new files
    pProperties.setProperty("modified", "yes-new-files");
    pProperties.save();

    // old files can be removed and new files renamed
    renameNewToCurrent(sFileScript);
    renameNewToCurrent(sFileBackup);

    // now its done completely
    pProperties.setProperty("modified", "no");
    pProperties.setProperty("version", jdbcDriver.VERSION);
    pProperties.setProperty("hsqldb.compatible_version", "1.7.0");
    pProperties.save();
    pProperties.close();

    if (compact) {

      // stop the runner thread of this process (just for security)
      stop();

      // delete the .data so then a new file is created
      (new File(sFileCache)).delete();
      (new File(sFileBackup)).delete();

      // tony_lai@users 20020820
      // The database re-open and close has been moved to
      // Database#close(int closemode) for saving memory usage.
    }
  }