/** Drops from this Database any temporary tables owned by the specified Session. */
  void dropTempTables(Session ownerSession) {

    int i = tTable.size();

    while (i-- > 0) {
      Table toDrop = (Table) tTable.get(i);

      if (toDrop.isTemp() && toDrop.getOwnerSessionId() != ownerSession.getId()) {
        tTable.remove(i);
      }
    }
  }
  /** 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();
  }
  public DataFileCacheSession getResultCache() {

    if (resultCache == null) {
      String path = database.getTempDirectoryPath();

      if (path == null) {
        return null;
      }

      try {
        resultCache =
            new DataFileCacheSession(database, path + "/session_" + Long.toString(session.getId()));

        resultCache.open(false);
      } catch (Throwable t) {
        return null;
      }
    }

    return resultCache;
  }
Esempio n. 5
0
  /**
   * Method declaration
   *
   * @param c
   * @param s
   * @throws SQLException
   */
  void write(Session c, String s) throws SQLException {

    if (bRestoring || s == null || s.length() == 0) {
      return;
    }

    if (!bReadOnly) {
      int id = 0;

      if (c != null) {
        id = c.getId();
      }

      if (id != mLastId) {
        s = "/*C" + id + "*/" + s;
        mLastId = id;
      }

      try {
        writeLine(wScript, s);

        if (bWriteDelay) {
          bNeedFlush = true;
        } else {
          wScript.flush();
        }
      } catch (IOException e) {
        throw Trace.error(Trace.FILE_IO_ERROR, sFileScript);
      }

      // fredt@users - todo - eliminate new File() calls
      if (iLogSize > 0 && iLogCount++ > 100) {
        iLogCount = 0;

        if (scriptChecker.length() > iLogSize * 1024 * 1024) {
          checkpoint(false);
        }
      }
    }
  }
  /** 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;
  }
Esempio n. 7
0
  /**
   * Method declaration
   *
   * @throws SQLException
   */
  private void runScript() throws SQLException {

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

    if (!(new File(sFileScript)).exists()) {
      return;
    }

    bRestoring = true;

    dDatabase.setReferentialIntegrity(false);

    HsqlArrayList session = new HsqlArrayList();

    session.add(sysSession);

    Session current = sysSession;

    try {
      long time = 0;

      if (Trace.TRACE) {
        time = System.currentTimeMillis();
      }

      LineNumberReader r = new LineNumberReader(new FileReader(sFileScript));

      while (true) {
        String s = readLine(r);

        if (s == null) {
          break;
        }

        if (s.startsWith("/*C")) {
          int id = Integer.parseInt(s.substring(3, s.indexOf('*', 4)));

          if (id >= session.size()) {
            session.setSize(id + 1);
          }

          current = (Session) session.get(id);

          if (current == null) {
            current = new Session(sysSession, id);

            session.set(id, current);
            dDatabase.registerSession(current);
          }

          s = s.substring(s.indexOf('/', 1) + 1);
        }

        if (s.length() != 0) {
          Result result = dDatabase.execute(s, current);

          if ((result != null) && (result.iMode == Result.ERROR)) {
            throw (Trace.getError(result.errorCode, result.sError));
          }
        }

        if (s.equals("DISCONNECT")) {
          int id = current.getId();

          current = new Session(sysSession, id);

          session.set(id, current);
        }
      }

      r.close();

      for (int i = 0; i < session.size(); i++) {
        current = (Session) session.get(i);

        if (current != null) {
          current.rollback();
        }
      }

      if (Trace.TRACE) {
        Trace.trace(time - System.currentTimeMillis());
      }
    } catch (IOException e) {
      throw Trace.error(Trace.FILE_IO_ERROR, sFileScript + " " + e);
    }

    dDatabase.setReferentialIntegrity(true);

    bRestoring = false;
  }