/**
   * Runs the setInitializingFailed prepared statement. <code>
   *  		UPDATE SERVER SET INITIALIZED=NULL
   * </code>
   *
   * @param stmtProvider factory and cache of PreparedStatments
   * @param connection connection to underlying database
   * @return int
   * @throws org.openanzo.jdbc.utils.RdbException
   */
  public static int setInitializingFailed(
      final org.openanzo.jdbc.utils.PreparedStatementProvider stmtProvider,
      final java.sql.Connection connection)
      throws org.openanzo.jdbc.utils.RdbException {
    java.sql.PreparedStatement ps = null;
    // long startTimer=System.currentTimeMillis();
    try {
      ps = stmtProvider.getPreparedSQLStatement(setInitializingFailed, new String[] {}, connection);
      int counter = 0;
      try {
        counter = ps.executeUpdate();
      } catch (java.sql.SQLException sqle) {
        if (sqle.getErrorCode() == 1205) {
          int retries = 0;
          while (retries < 5) {
            try {
              Thread.sleep(5000);
            } catch (InterruptedException ie) {
              throw sqle;
            }
            try {
              counter = ps.executeUpdate();
              break;
            } catch (java.sql.SQLException sqleInner) {
              if (sqleInner.getErrorCode() == 1205) {
                retries++;
              } else {
                throw sqleInner;
              }
            }
          }
          if (retries >= 5) {
            throw sqle;
          }
        } else {
          throw sqle;
        }
      }
      return counter;

    } catch (java.sql.SQLException e) {
      throw new org.openanzo.jdbc.utils.RdbException(
          org.openanzo.exceptions.ExceptionConstants.RDB.FAILED_EXECUTING_SQL,
          e,
          "setInitializingFailed",
          stmtProvider.getSqlString(setInitializingFailed),
          "",
          "");
    } finally {
      if (ps != null) {
        try {
          ps.close();
        } catch (java.sql.SQLException sqle) {
          if (log.isDebugEnabled())
            log.debug(
                org.openanzo.exceptions.LogUtils.RDB_MARKER,
                "Error closing prepared statement",
                sqle);
        }
      }
      // long endtimer=(System.currentTimeMillis()-startTimer);
      // if(endtimer>CUTOFF)System.out.println("[setInitializingFailed]"+endtimer);
    }
  }
  /**
   * Runs the lockTable prepared statement. <code>
   *  		LOCK TABLE {0} {1}
   * </code>
   *
   * @param stmtProvider factory and cache of PreparedStatments
   * @param connection connection to underlying database
   * @param tableName template parameter
   * @param tableLocksExtra template parameter
   * @throws org.openanzo.jdbc.utils.RdbException
   */
  public static void lockTable(
      final org.openanzo.jdbc.utils.PreparedStatementProvider stmtProvider,
      final java.sql.Connection connection,
      String tableName,
      String tableLocksExtra)
      throws org.openanzo.jdbc.utils.RdbException {
    java.sql.PreparedStatement ps = null;
    // long startTimer=System.currentTimeMillis();
    try {
      ps =
          stmtProvider.getPreparedSQLStatement(
              lockTable, new String[] {tableName, tableLocksExtra}, connection);
      try {
        ps.execute();
      } catch (java.sql.SQLException sqle) {
        if (sqle.getErrorCode() == 1205) {
          int retries = 0;
          while (retries < 5) {
            try {
              Thread.sleep(5000);
            } catch (InterruptedException ie) {
              throw sqle;
            }
            try {
              ps.execute();
              break;
            } catch (java.sql.SQLException sqleInner) {
              if (sqleInner.getErrorCode() == 1205) {
                retries++;
              } else {
                throw sqleInner;
              }
            }
          }
          if (retries >= 5) {
            throw sqle;
          }
        } else {
          throw sqle;
        }
      }

    } catch (java.sql.SQLException e) {
      throw new org.openanzo.jdbc.utils.RdbException(
          org.openanzo.exceptions.ExceptionConstants.RDB.FAILED_EXECUTING_SQL,
          e,
          "lockTable",
          stmtProvider.getSqlString(lockTable),
          "",
          ""
              + "tableName="
              + ((tableName != null) ? tableName.toString() : "null")
              + ","
              + "tableLocksExtra="
              + ((tableLocksExtra != null) ? tableLocksExtra.toString() : "null"));
    } finally {
      if (ps != null) {
        try {
          ps.close();
        } catch (java.sql.SQLException sqle) {
          if (log.isDebugEnabled())
            log.debug(
                org.openanzo.exceptions.LogUtils.RDB_MARKER,
                "Error closing prepared statement",
                sqle);
        }
      }
      // long endtimer=(System.currentTimeMillis()-startTimer);
      // if(endtimer>CUTOFF)System.out.println("[lockTable]"+endtimer);
    }
  }
  /**
   * Runs the getServerVersion prepared statement. <code>
   *  		SELECT VERSION FROM SERVER;
   * </code>
   *
   * @param stmtProvider factory and cache of PreparedStatments
   * @param connection connection to underlying database
   * @return Long
   * @throws org.openanzo.jdbc.utils.RdbException
   */
  public static Long getServerVersion(
      final org.openanzo.jdbc.utils.PreparedStatementProvider stmtProvider,
      final java.sql.Connection connection)
      throws org.openanzo.jdbc.utils.RdbException {
    java.sql.PreparedStatement ps = null;
    // long startTimer=System.currentTimeMillis();
    try {
      ps = stmtProvider.getPreparedSQLStatement(getServerVersion, new String[] {}, connection);
      java.sql.ResultSet rs = null;
      try {
        try {
          rs = ps.executeQuery();
        } catch (java.sql.SQLException sqle) {
          if (sqle.getErrorCode() == 1205) {
            int retries = 0;
            while (retries < 5) {
              try {
                Thread.sleep(5000);
              } catch (InterruptedException ie) {
                throw sqle;
              }
              try {
                rs = ps.executeQuery();
                break;
              } catch (java.sql.SQLException sqleInner) {
                if (sqleInner.getErrorCode() == 1205) {
                  retries++;
                } else {
                  throw sqleInner;
                }
              }
            }
            if (retries >= 5) {
              throw sqle;
            }
          } else {
            throw sqle;
          }
        }
        if (!rs.next()) return null;
        Long val = rs.getLong(1);
        return val;
      } finally {
        if (rs != null) {
          try {
            rs.close();
          } catch (java.sql.SQLException sqle) {
            if (log.isDebugEnabled())
              log.debug(
                  org.openanzo.exceptions.LogUtils.RDB_MARKER, "Error closing result set", sqle);
          }
        }
      }

    } catch (java.sql.SQLException e) {
      throw new org.openanzo.jdbc.utils.RdbException(
          org.openanzo.exceptions.ExceptionConstants.RDB.FAILED_EXECUTING_SQL,
          e,
          "getServerVersion",
          stmtProvider.getSqlString(getServerVersion),
          "",
          "");
    } finally {
      if (ps != null) {
        try {
          ps.close();
        } catch (java.sql.SQLException sqle) {
          if (log.isDebugEnabled())
            log.debug(
                org.openanzo.exceptions.LogUtils.RDB_MARKER,
                "Error closing prepared statement",
                sqle);
        }
      }
      // long endtimer=(System.currentTimeMillis()-startTimer);
      // if(endtimer>CUTOFF)System.out.println("[getServerVersion]"+endtimer);
    }
  }