/** Initializes this connection. */
  private void init() {

    runnerThread = Thread.currentThread();
    keepAlive = true;

    try {
      socket.setTcpNoDelay(true);

      dataInput = new DataInputStream(socket.getInputStream());
      dataOutput = new BufferedOutputStream(socket.getOutputStream());

      Result resultIn = Result.read(rowIn, dataInput);
      Result resultOut;

      try {
        int dbIndex = ArrayUtil.find(server.dbAlias, resultIn.subSubString);

        dbID = server.dbID[dbIndex];
        user = resultIn.getMainString();
        password = resultIn.getSubString();

        if (!server.isSilent()) {
          server.printWithThread(mThread + ":trying to connect user " + user);
        }

        session =
            DatabaseManager.newSession(dbID, resultIn.getMainString(), resultIn.getSubString());
        resultOut = new Result(ResultConstants.UPDATECOUNT);
        resultOut.databaseID = session.getDatabase().databaseID;
        resultOut.sessionID = session.getId();
      } catch (HsqlException e) {
        session = null;
        resultOut = new Result(e, null);
      } catch (ArrayIndexOutOfBoundsException e) {
        session = null;
        resultOut = new Result(Trace.error(Trace.DATABASE_NOT_EXISTS), resultIn.subSubString);
      }

      Result.write(resultOut, rowOut, dataOutput);

      return;
    } catch (Exception e) {
      server.printWithThread(mThread + ":couldn't connect " + user);
    }

    close();
  }
  /** Initializes this connection. */
  private void init() {

    runnerThread = Thread.currentThread();
    keepAlive = true;

    try {
      socket.setTcpNoDelay(true);

      dataInput = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
      dataOutput = new BufferedOutputStream(socket.getOutputStream());

      Result resultIn = Result.read(rowIn, dataInput);
      Result resultOut;

      try {
        dbID = server.getDBID(resultIn.subSubString);
        user = resultIn.getMainString();
        password = resultIn.getSubString();

        if (!server.isSilent()) {
          server.printWithThread(mThread + ":trying to connect user " + user);
        }

        session =
            DatabaseManager.newSession(dbID, resultIn.getMainString(), resultIn.getSubString());
        resultOut = new Result(ResultConstants.UPDATECOUNT);
        resultOut.databaseID = session.getDatabase().databaseID;
        resultOut.sessionID = session.getId();
      } catch (HsqlException e) {
        session = null;
        resultOut = new Result(e, null);
      } catch (RuntimeException e) {
        session = null;
        resultOut = new Result(e, null);
      }

      Result.write(resultOut, rowOut, dataOutput);

      return;
    } catch (Exception e) {
      server.printWithThread(mThread + ":couldn't connect " + user);
    }

    close();
  }
  /** Used by pooled connections to close the existing SQL session and open a new one. */
  private Result resetSession() {

    Result resultOut;

    if (!server.isSilent()) {
      server.printWithThread(mThread + ":trying to connect user " + user);
    }

    try {
      session.close();

      session = DatabaseManager.newSession(dbID, user, password);
      resultOut = new Result(ResultConstants.UPDATECOUNT);
      resultOut.databaseID = session.getDatabase().databaseID;
      resultOut.sessionID = session.getId();
    } catch (HsqlException e) {
      session = null;
      resultOut = new Result(e, null);
    }

    return resultOut;
  }
  /** Initializes this connection and runs the request handling loop until closed. */
  public void run() {

    init();

    if (session != null) {
      try {
        while (keepAlive) {
          Result resultIn = Result.read(rowIn, dataInput);

          server.printRequest(mThread, resultIn);

          Result resultOut;

          if (resultIn.mode == ResultConstants.HSQLRESETSESSION) {
            resultOut = resetSession();
          } else {
            resultOut = session.execute(resultIn);
          }

          Result.write(resultOut, rowOut, dataOutput);
          rowOut.setBuffer(mainBuffer);
          rowIn.resetRow(mainBuffer.length);
        }
      } catch (IOException e) {

        // fredt - is thrown when connection drops
        server.printWithThread(mThread + ":disconnected " + user);
      } catch (HsqlException e) {

        // fredt - is thrown while constructing the result
        server.printStackTrace(e);
      }
    }

    close();
  }