session.java

来自「非常棒的java数据库」· Java 代码 · 共 759 行 · 第 1/2 页

JAVA
759
字号
     * @param row the row
     */
    public void log(Table table, short type, Row row) throws SQLException {
        log(new UndoLogRecord(table, type, row));
    }

    private void log(UndoLogRecord log) throws SQLException {
        // called _after_ the row was inserted successfully into the table,
        // otherwise rollback will try to rollback a not-inserted row
        if (SysProperties.CHECK) {
            int lockMode = database.getLockMode();
            if (lockMode != Constants.LOCK_MODE_OFF && !database.isMultiVersion()) {
                if (locks.indexOf(log.getTable()) < 0 && log.getTable().getTableType() != Table.TABLE_LINK) {
                    throw Message.getInternalError();
                }
            }
        }
        if (undoLogEnabled) {
            undoLog.add(log);
        }
    }

    public void unlockReadLocks() {
        if (database.isMultiVersion()) {
            // MVCC: keep shared locks (insert / update / delete)
            return;
        }
        for (int i = 0; i < locks.size(); i++) {
            Table t = (Table) locks.get(i);
            if (!t.isLockedExclusively()) {
                synchronized (database) {
                    t.unlock(this);
                    locks.remove(i);
                }
                i--;
            }
        }
    }

    private void unlockAll() throws SQLException {
        if (SysProperties.CHECK) {
            if (undoLog.size() > 0) {
                throw Message.getInternalError();
            }
        }
        synchronized (database) {
            for (int i = 0; i < locks.size(); i++) {
                Table t = (Table) locks.get(i);
                t.unlock(this);
            }
            locks.clear();
        }
        savepoints = null;
    }

    private void cleanTempTables(boolean closeSession) throws SQLException {
        if (localTempTables != null && localTempTables.size() > 0) {
            ObjectArray list = new ObjectArray(localTempTables.values());
            for (int i = 0; i < list.size(); i++) {
                Table table = (Table) list.get(i);
                if (closeSession || table.isOnCommitDrop()) {
                    table.setModified();
                    localTempTables.remove(table.getName());
                    table.removeChildrenAndResources(this);
                } else if (table.isOnCommitTruncate()) {
                    table.truncate(this);
                }
            }
        }
    }

    public Random getRandom() {
        if (random == null) {
            random = new Random();
        }
        return random;
    }

    public Trace getTrace() {
        if (traceModuleName == null) {
            traceModuleName = Trace.JDBC + "[" + id + "]";
        }
        if (closed) {
            return new TraceSystem(null, false).getTrace(traceModuleName);
        }
        return database.getTrace(traceModuleName);
    }

    public void setLastIdentity(Value last) {
        this.lastIdentity = last;
    }

    public Value getLastIdentity() {
        return lastIdentity;
    }

    public void addLogPos(int logId, int pos) {
        if (firstUncommittedLog == LogSystem.LOG_WRITTEN) {
            firstUncommittedLog = logId;
            firstUncommittedPos = pos;
        }
    }

    public int getFirstUncommittedLog() {
        return firstUncommittedLog;
    }

    public int getFirstUncommittedPos() {
        return firstUncommittedPos;
    }

    public void setAllCommitted() {
        firstUncommittedLog = LogSystem.LOG_WRITTEN;
        firstUncommittedPos = LogSystem.LOG_WRITTEN;
    }

    private boolean containsUncommitted() {
        return firstUncommittedLog != LogSystem.LOG_WRITTEN;
    }

    public void addSavepoint(String name) {
        if (savepoints == null) {
            savepoints = new HashMap();
        }
        savepoints.put(name, ObjectUtils.getInteger(getLogId()));
    }

    public void rollbackToSavepoint(String name) throws SQLException {
        checkCommitRollback();        
        if (savepoints == null) {
            throw Message.getSQLException(ErrorCode.SAVEPOINT_IS_INVALID_1, name);
        }
        Integer id = (Integer) savepoints.get(name);
        if (id == null) {
            throw Message.getSQLException(ErrorCode.SAVEPOINT_IS_INVALID_1, name);
        }
        int i = id.intValue();
        rollbackTo(i);
    }

    public void prepareCommit(String transactionName) throws SQLException {
        if (containsUncommitted()) {
            // need to commit even if rollback is not possible (create/drop
            // table and so on)
            logSystem.prepareCommit(this, transactionName);
        }
        currentTransactionName = transactionName;
    }

    public void setPreparedTransaction(String transactionName, boolean commit) throws SQLException {
        if (currentTransactionName != null && currentTransactionName.equals(transactionName)) {
            if (commit) {
                commit(false);
            } else {
                rollback();
            }
        } else {
            ObjectArray list = logSystem.getInDoubtTransactions();
            int state = commit ? InDoubtTransaction.COMMIT : InDoubtTransaction.ROLLBACK;
            boolean found = false;
            for (int i = 0; list != null && i < list.size(); i++) {
                InDoubtTransaction p = (InDoubtTransaction) list.get(i);
                if (p.getTransaction().equals(transactionName)) {
                    p.setState(state);
                    found = true;
                    break;
                }
            }
            if (!found) {
                throw Message.getSQLException(ErrorCode.TRANSACTION_NOT_FOUND_1, transactionName);
            }
        }
    }

    public boolean isClosed() {
        return closed;
    }

    public void setThrottle(int throttle) {
        this.throttle = throttle;
    }

    public void throttle() {
        if (throttle == 0) {
            return;
        }
        long time = System.currentTimeMillis();
        if (lastThrottle + Constants.THROTTLE_DELAY > time) {
            return;
        }
        lastThrottle = time + throttle;
        try {
            Thread.sleep(throttle);
        } catch (Exception e) {
            // ignore
        }
    }

    public void setCurrentCommand(Command command, long startTime) {
        this.currentCommand = command;
        this.currentCommandStart = startTime;
        if (queryTimeout > 0) {
            cancelAt = startTime + queryTimeout;
        }
    }

    public void checkCancelled() throws SQLException {
        throttle();
        if (cancelAt == 0) {
            return;
        }
        long time = System.currentTimeMillis();
        if (time >= cancelAt) {
            cancelAt = 0;
            throw Message.getSQLException(ErrorCode.STATEMENT_WAS_CANCELLED);
        }
    }

    public Command getCurrentCommand() {
        return currentCommand;
    }

    public long getCurrentCommandStart() {
        return currentCommandStart;
    }

    public boolean getAllowLiterals() {
        return allowLiterals;
    }

    public void setAllowLiterals(boolean b) {
        this.allowLiterals = b;
    }

    public void setCurrentSchema(Schema schema) {
        this.currentSchemaName = schema.getName();
    }

    public String getCurrentSchemaName() {
        return currentSchemaName;
    }

    public JdbcConnection createConnection(boolean columnList) throws SQLException {
        String url;
        if (columnList) {
            url = Constants.CONN_URL_COLUMNLIST;
        } else {
            url = Constants.CONN_URL_INTERNAL;
        }
        return new JdbcConnection(this, getUser().getName(), url);
    }

    public DataHandler getDataHandler() {
        return database;
    }

    public void unlinkAtCommit(Value v) {
        if (unlinkMap == null) {
            unlinkMap = new HashMap();
        }
        unlinkMap.put(v.toString(), v);
    }

    public void unlinkAtCommitStop(Value v) {
        if (unlinkMap != null) {
            unlinkMap.remove(v.toString());
        }
    }

    public String getNextTempViewName() {
        return "TEMP_VIEW_" + tempViewIndex++;
    }

    public void addProcedure(Procedure procedure) {
        if (procedures == null) {
            procedures = new HashMap();
        }
        procedures.put(procedure.getName(), procedure);
    }

    public void removeProcedure(String name) {
        if (procedures != null) {
            procedures.remove(name);
        }
    }

    public Procedure getProcedure(String name) {
        if (procedures == null) {
            return null;
        }
        return (Procedure) procedures.get(name);
    }

    public void setSchemaSearchPath(String[] schemas) {
        this.schemaSearchPath = schemas;
    }

    public String[] getSchemaSearchPath() {
        return schemaSearchPath;
    }

    public int hashCode() {
        return serialId;
    }

    public void setUndoLogEnabled(boolean b) {
        this.undoLogEnabled = b;
    }

    public boolean getUndoLogEnabled() {
        return undoLogEnabled;
    }

    public void begin() {
        autoCommitAtTransactionEnd = true;
        autoCommit = false;
    }

    public boolean getRollbackMode() {
        return rollbackMode;
    }

    public long getSessionStart() {
        return sessionStart;
    }

    public Table[] getLocks() {
        synchronized (database) {
            Table[] list = new Table[locks.size()];
            locks.toArray(list);
            return list;
        }
    }

    public void waitIfExclusiveModeEnabled() {
        while (true) {
            Session exclusive = database.getExclusiveSession();
            if (exclusive == null || exclusive == this) {
                break;
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                // ignore
            }
        }
    }

    public void addTemporaryResult(LocalResult result) {
        if (temporaryResults == null) {
            temporaryResults = new HashSet();
        }
        temporaryResults.add(result);
    }

    public void closeTemporaryResults() {
        if (temporaryResults != null) {
            for (Iterator it = temporaryResults.iterator(); it.hasNext();) {
                LocalResult result = (LocalResult) it.next();
                result.close();
            }
        }
    }

    public void setQueryTimeout(int queryTimeout) {
        int max = SysProperties.getMaxQueryTimeout();
        if (max != 0 && (max < queryTimeout || queryTimeout == 0)) {
            // the value must be at most max
            queryTimeout = max;
        }
        this.queryTimeout = queryTimeout;
    }

    public int getQueryTimeout() {
        return queryTimeout;
    }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?