📄 session.java
字号:
/* Copyright (c) 1995-2000, The Hypersonic SQL Group. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * Neither the name of the Hypersonic SQL Group nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE HYPERSONIC SQL GROUP, * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * This software consists of voluntary contributions made by many individuals * on behalf of the Hypersonic SQL Group. * * * For work added by the HSQL Development Group: * * Copyright (c) 2001-2005, The HSQL Development Group * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * Neither the name of the HSQL Development Group nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG, * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */package org.hsqldb;import java.sql.Date;import java.sql.Time;import java.sql.Timestamp;import org.hsqldb.HsqlNameManager.HsqlName;import org.hsqldb.jdbc.jdbcConnection;import org.hsqldb.lib.ArrayUtil;import org.hsqldb.lib.HashMappedList;import org.hsqldb.lib.HsqlArrayList;import org.hsqldb.lib.IntKeyHashMap;import org.hsqldb.lib.SimpleLog;import org.hsqldb.lib.java.JavaSystem;import org.hsqldb.store.ValuePool;// fredt@users 20020320 - doc 1.7.0 - update// fredt@users 20020315 - patch 1.7.0 - switch for scripting// fredt@users 20020130 - patch 476694 by velichko - transaction savepoints// additions in different parts to support savepoint transactions// fredt@users 20020910 - patch 1.7.1 by fredt - database readonly enforcement// fredt@users 20020912 - patch 1.7.1 by fredt - permanent internal connection// boucherb@users 20030512 - patch 1.7.2 - compiled statements// - session becomes execution hub// boucherb@users 20050510 - patch 1.7.2 - generalized Result packet passing// based command execution// - batch execution handling// fredt@users 20030628 - patch 1.7.2 - session proxy support// fredt@users 20040509 - patch 1.7.2 - SQL conformance for CURRENT_TIMESTAMP and other datetime functions/** * Implementation of a user session with the database. In 1.7.2 Session * becomes the public interface to an HSQLDB database, accessed locally or * remotely via SessionInterface. * * When as Session is closed, all references to internal engine objects are * set to null. But the session id and scripting mode may still be used for * scripting * * New class based based on original Hypersonic code. * Extensively rewritten and extended in successive versions of HSQLDB. * * @author Thomas Mueller (Hypersonic SQL Group) * @version 1.8.0 * @since 1.7.0 */public class Session implements SessionInterface { // private volatile boolean isAutoCommit; private volatile boolean isReadOnly; private volatile boolean isClosed; // Database database; private User user; HsqlArrayList rowActionList; private boolean isNestedTransaction; private int nestedOldTransIndex; int isolationMode = SessionInterface.TX_READ_COMMITTED; long actionTimestamp; long transactionTimestamp; private int currentMaxRows; private int sessionMaxRows; private Number lastIdentity = ValuePool.getInt(0); private final int sessionId; HashMappedList savepoints; private boolean script; private Tokenizer tokenizer; private Parser parser; static final Result emptyUpdateCount = new Result(ResultConstants.UPDATECOUNT); // private jdbcConnection intConnection; // schema public HsqlName currentSchema; public HsqlName loggedSchema; private HsqlName oldSchema; // query processing boolean isProcessingScript; boolean isProcessingLog; // two types of temp tables private IntKeyHashMap indexArrayMap; private IntKeyHashMap indexArrayKeepMap; /** @todo fredt - clarify in which circumstances Session has to disconnect */ Session getSession() { return this; } /** * Constructs a new Session object. * * @param db the database to which this represents a connection * @param user the initial user * @param autocommit the initial autocommit value * @param readonly the initial readonly value * @param id the session identifier, as known to the database */ Session(Database db, User user, boolean autocommit, boolean readonly, int id) { sessionId = id; database = db; this.user = user; rowActionList = new HsqlArrayList(true); savepoints = new HashMappedList(4); isAutoCommit = autocommit; isReadOnly = readonly; dbCommandInterpreter = new DatabaseCommandInterpreter(this); compiledStatementExecutor = new CompiledStatementExecutor(this); compiledStatementManager = db.compiledStatementManager; tokenizer = new Tokenizer(); parser = new Parser(this, database, tokenizer); resetSchema(); } void resetSchema() { HsqlName initialSchema = user.getInitialSchema(); currentSchema = ((initialSchema == null) ? database.schemaManager.getDefaultSchemaHsqlName() : initialSchema); } /** * Retrieves the session identifier for this Session. * * @return the session identifier for this Session */ public int getId() { return sessionId; } /** * Closes this Session. */ public void close() { if (isClosed) { return; } synchronized (database) { // test again inside block if (isClosed) { return; } database.sessionManager.removeSession(this); rollback(); try { database.logger.writeToLog(this, Token.T_DISCONNECT); } catch (HsqlException e) {} clearIndexRoots(); clearIndexRootsKeep(); compiledStatementManager.removeSession(sessionId); database.closeIfLast(); database = null; user = null; rowActionList = null; savepoints = null; intConnection = null; compiledStatementExecutor = null; compiledStatementManager = null; dbCommandInterpreter = null; lastIdentity = null; isClosed = true; } } /** * Retrieves whether this Session is closed. * * @return true if this Session is closed */ public boolean isClosed() { return isClosed; } public void setIsolation(int level) throws HsqlException { isolationMode = level; } public int getIsolation() throws HsqlException { return isolationMode; } /** * Setter for iLastIdentity attribute. * * @param i the new value */ void setLastIdentity(Number i) { lastIdentity = i; } /** * Getter for iLastIdentity attribute. * * @return the current value */ Number getLastIdentity() { return lastIdentity; } /** * Retrieves the Database instance to which this * Session represents a connection. * * @return the Database object to which this Session is connected */ Database getDatabase() { return database; } /** * Retrieves the name, as known to the database, of the * user currently controlling this Session. * * @return the name of the user currently connected within this Session */ String getUsername() { return user.getName(); } /** * Retrieves the User object representing the user currently controlling * this Session. * * @return this Session's User object */ public User getUser() { return user; } /** * Sets this Session's User object to the one specified by the * user argument. * * @param user the new User object for this session */ void setUser(User user) { this.user = user; } int getMaxRows() { return currentMaxRows; } int getSQLMaxRows() { return sessionMaxRows; } /** * The SQL command SET MAXROWS n will override the Statement.setMaxRows(n) * until SET MAXROWS 0 is issued. * * NB this is dedicated to the SET MAXROWS sql statement and should not * otherwise be called. (fredt@users) */ void setSQLMaxRows(int rows) { currentMaxRows = sessionMaxRows = rows; } /** * Checks whether this Session's current User has the privileges of * the ADMIN role. * * @throws HsqlException if this Session's User does not have the * privileges of the ADMIN role. */ void checkAdmin() throws HsqlException { user.checkAdmin(); } /** * Checks whether this Session's current User has the set of rights * specified by the right argument, in relation to the database * object identifier specified by the object argument. * * @param object the database object to check * @param right the rights to check for * @throws HsqlException if the Session User does not have such rights */ void check(HsqlName object, int right) throws HsqlException { user.check(object, right); } /** * Checks the rights on a function * * @param object the function * @throws HsqlException if the Session User does not have such rights */ void check(String object) throws HsqlException { user.check(object); } /** * This is used for reading - writing to existing tables. * @throws HsqlException */ void checkReadWrite() throws HsqlException { if (isReadOnly) { throw Trace.error(Trace.DATABASE_IS_READONLY); } } /** * This is used for creating new database objects such as tables. * @throws HsqlException */ void checkDDLWrite() throws HsqlException { if (database.isFilesReadOnly() &&!user.isSys()) { throw Trace.error(Trace.DATABASE_IS_READONLY); } } /** * Adds a single-row deletion step to the transaction UNDO buffer. * * @param table the table from which the row was deleted * @param row the deleted row * @throws HsqlException */ boolean addDeleteAction(Table table, Row row) throws HsqlException { if (!isAutoCommit || isNestedTransaction) { Transaction t = new Transaction(true, table, row, actionTimestamp); rowActionList.add(t); database.txManager.addTransaction(this, t); return true; } else { table.removeRowFromStore(row); } return false; } /** * Adds a single-row insertion step to the transaction UNDO buffer. * * @param table the table into which the row was inserted * @param row the inserted row * @throws HsqlException */ boolean addInsertAction(Table table, Row row) throws HsqlException { if (!isAutoCommit || isNestedTransaction) { Transaction t = new Transaction(false, table, row, actionTimestamp); rowActionList.add(t); database.txManager.addTransaction(this, t); return true; } else { table.commitRowToStore(row); } return false; } /** * Setter for the autocommit attribute. * * @param autocommit the new value * @throws HsqlException */ public void setAutoCommit(boolean autocommit) { if (isClosed) { return; } synchronized (database) { if (autocommit != isAutoCommit) { commit(); isAutoCommit = autocommit; try { database.logger.writeToLog(this, getAutoCommitStatement()); } catch (HsqlException e) {} } } } public void startPhasedTransaction() throws HsqlException {} public void prepareCommit() throws HsqlException {} /** * Commits any uncommited transaction this Session may have open * * @throws HsqlException */ public void commit() { if (isClosed) { return; } synchronized (database) { if (!rowActionList.isEmpty()) { try { database.logger.writeCommitStatement(this); } catch (HsqlException e) {} } database.txManager.commit(this); clearIndexRoots(); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -