📄 session.java
字号:
/** * Rolls back any uncommited transaction this Session may have open. * * @throws HsqlException */ public void rollback() { if (isClosed) { return; } synchronized (database) { if (rowActionList.size() != 0) { try { database.logger.writeToLog(this, Token.T_ROLLBACK); } catch (HsqlException e) {} } database.txManager.rollback(this); clearIndexRoots(); } } /** * No-op in this implementation */ public void resetSession() throws HsqlException { throw new HsqlException("", "", 0); } /** * Implements a transaction SAVEPOINT. A new SAVEPOINT with the * name of an existing one replaces the old SAVEPOINT. * * @param name of the savepoint * @throws HsqlException if there is no current transaction */ void savepoint(String name) throws HsqlException { savepoints.remove(name); savepoints.add(name, ValuePool.getInt(rowActionList.size())); try { database.logger.writeToLog(this, Token.T_SAVEPOINT + " " + name); } catch (HsqlException e) {} } /** * Implements a partial transaction ROLLBACK. * * @param name Name of savepoint that was marked before by savepoint() * call * @throws HsqlException */ void rollbackToSavepoint(String name) throws HsqlException { if (isClosed) { return; } try { database.logger.writeToLog(this, Token.T_ROLLBACK + " " + Token.T_TO + " " + Token.T_SAVEPOINT + " " + name); } catch (HsqlException e) {} database.txManager.rollbackSavepoint(this, name); } /** * Implements release of named SAVEPOINT. * * @param name Name of savepoint that was marked before by savepoint() * call * @throws HsqlException if name does not correspond to a savepoint */ void releaseSavepoint(String name) throws HsqlException { // remove this and all later savepoints int index = savepoints.getIndex(name); Trace.check(index >= 0, Trace.SAVEPOINT_NOT_FOUND, name); while (savepoints.size() > index) { savepoints.remove(savepoints.size() - 1); } } /** * Starts a nested transaction. * * @throws HsqlException */ void beginNestedTransaction() throws HsqlException { if (isNestedTransaction) { Trace.doAssert(false, "beginNestedTransaction"); } nestedOldTransIndex = rowActionList.size(); isNestedTransaction = true; if (isAutoCommit) { try { database.logger.writeToLog(this, "SET AUTOCOMMIT FALSE"); } catch (HsqlException e) {} } } /** * @todo -- fredt 20050604 - if this method is called after an out of memory * error during update, the next block might throw out of memory too and as * a result inNestedTransaction remains true and no further update * is possible. The session must be closed at that point by the user * application. */ /** * Ends a nested transaction. * * @param rollback true to roll back or false to commit the nested transaction * @throws HsqlException */ void endNestedTransaction(boolean rollback) throws HsqlException { if (!isNestedTransaction) { Trace.doAssert(false, "endNestedTransaction"); } if (rollback) { database.txManager.rollbackTransactions(this, nestedOldTransIndex, true); } // reset after the rollback isNestedTransaction = false; if (isAutoCommit) { database.txManager.commit(this); try { database.logger.writeToLog(this, "SET AUTOCOMMIT TRUE"); } catch (HsqlException e) {} } } /** * Setter for readonly attribute. * * @param readonly the new value */ public void setReadOnly(boolean readonly) throws HsqlException { if (!readonly && database.databaseReadOnly) { throw Trace.error(Trace.DATABASE_IS_READONLY); } isReadOnly = readonly; } /** * Getter for readonly attribute. * * @return the current value */ public boolean isReadOnly() { return isReadOnly; } /** * Getter for nestedTransaction attribute. * * @return the current value */ boolean isNestedTransaction() { return isNestedTransaction; } /** * Getter for autoCommit attribute. * * @return the current value */ public boolean isAutoCommit() { return isAutoCommit; } /** * A switch to set scripting on the basis of type of statement executed. * A method in DatabaseCommandInterpreter.java sets this value to false * before other methods are called to act on an SQL statement, which may * set this to true. Afterwards the method reponsible for logging uses * getScripting() to determine if logging is required for the executed * statement. (fredt@users) * * @param script The new scripting value */ void setScripting(boolean script) { this.script = script; } /** * Getter for scripting attribute. * * @return scripting for the last statement. */ boolean getScripting() { return script; } public String getAutoCommitStatement() { return isAutoCommit ? "SET AUTOCOMMIT TRUE" : "SET AUTOCOMMIT FALSE"; } /** * Retrieves an internal Connection object equivalent to the one * that created this Session. * * @return internal connection. */ jdbcConnection getInternalConnection() throws HsqlException { if (intConnection == null) { intConnection = new jdbcConnection(this); } return intConnection; }// boucherb@users 20020810 metadata 1.7.2//---------------------------------------------------------------- private final long connectTime = System.currentTimeMillis();// more effecient for MetaData concerns than checkAdmin /** * Getter for admin attribute. * * @ return the current value */ boolean isAdmin() { return user.isAdmin(); } /** * Getter for connectTime attribute. * * @return the value */ long getConnectTime() { return connectTime; } /** * Getter for transactionSise attribute. * * @return the current value */ int getTransactionSize() { return rowActionList.size(); } /** * Retrieves whether the database object identifier by the dbobject * argument is accessible by the current Session User. * * @return true if so, else false */ boolean isAccessible(String dbobject) throws HsqlException { return user.isAccessible(dbobject); } boolean isAccessible(HsqlName dbobject) throws HsqlException { return user.isAccessible(dbobject); }// boucherb@users 20030417 - patch 1.7.2 - compiled statement support//------------------------------------------------------------------- DatabaseCommandInterpreter dbCommandInterpreter; CompiledStatementExecutor compiledStatementExecutor; CompiledStatementManager compiledStatementManager; CompiledStatement sqlCompileStatement(String sql) throws HsqlException { parser.reset(sql); CompiledStatement cs; int brackets = 0; String token = tokenizer.getString(); int cmd = Token.get(token); switch (cmd) { case Token.OPENBRACKET : { brackets = parser.parseOpenBracketsSelect() + 1; } case Token.SELECT : { cs = parser.compileSelectStatement(brackets); break; } case Token.INSERT : { cs = parser.compileInsertStatement(); break; } case Token.UPDATE : { cs = parser.compileUpdateStatement(); break; } case Token.DELETE : { cs = parser.compileDeleteStatement(); break; } case Token.CALL : { cs = parser.compileCallStatement(); break; } default : { // DDL statements cs = new CompiledStatement(currentSchema); break; } } // In addition to requiring that the compilation was successful, // we also require that the submitted sql represents a _single_ // valid DML or DDL statement. We do not check the DDL yet. // fredt - now accepts semicolon and whitespace at the end of statement // fredt - investigate if it should or not for prepared statements if (cs.type != CompiledStatement.DDL) { while (tokenizer.getPosition() < tokenizer.getLength()) { token = tokenizer.getString(); if (token.length() != 0 &&!token.equals(Token.T_SEMICOLON)) { throw Trace.error(Trace.UNEXPECTED_TOKEN, token); } } } // - need to be able to key cs against its sql in statement pool // - also need to be able to revalidate its sql occasionally cs.sql = sql; return cs; } /** * Executes the command encapsulated by the cmd argument. * * @param cmd the command to execute * @return the result of executing the command */ public Result execute(Result cmd) { try { if (isClosed) { Trace.check(false, Trace.ACCESS_IS_DENIED, Trace.getMessage(Trace.Session_execute)); } } catch (Throwable t) { return new Result(t, null); } synchronized (database) { int type = cmd.mode; if (sessionMaxRows == 0) { currentMaxRows = cmd.updateCount; } // we simply get the next system change number - no matter what type of query actionTimestamp = database.txManager.nextActionTimestamp(); JavaSystem.gc(); switch (type) { case ResultConstants.SQLEXECUTE : { Result resultout = sqlExecute(cmd); resultout = performPostExecute(resultout); return resultout; } case ResultConstants.BATCHEXECUTE : { Result resultout = sqlExecuteBatch(cmd); resultout = performPostExecute(resultout); return resultout; } case ResultConstants.SQLEXECDIRECT : { Result resultout = sqlExecuteDirectNoPreChecks(cmd.getMainString()); resultout = performPostExecute(resultout); return resultout; } case ResultConstants.BATCHEXECDIRECT : { Result resultout = sqlExecuteBatchDirect(cmd); resultout = performPostExecute(resultout); return resultout; } case ResultConstants.SQLPREPARE : { CompiledStatement cs; try { cs = compiledStatementManager.compile( this, cmd.getMainString()); } catch (Throwable t) { return new Result(t, cmd.getMainString()); } Result rmd = cs.describeResult(); Result pmd = cs.describeParameters(); return Result.newPrepareResponse(cs.id, rmd, pmd); } case ResultConstants.SQLFREESTMT : { compiledStatementManager.freeStatement( cmd.getStatementID(), sessionId, false); return emptyUpdateCount; } case ResultConstants.GETSESSIONATTR : { return getAttributes(); } case ResultConstants.SETSESSIONATTR : { return setAttributes(cmd); } case ResultConstants.SQLENDTRAN : { switch (cmd.getEndTranType()) { case ResultConstants.COMMIT : commit(); break; case ResultConstants.ROLLBACK : rollback(); break; case ResultConstants.SAVEPOINT_NAME_RELEASE : try { String name = cmd.getMainString(); releaseSavepoint(name); } catch (Throwable t) { return new Result(t, null); } break; case ResultConstants.SAVEPOINT_NAME_ROLLBACK : try { rollbackToSavepoint(cmd.getMainString()); } catch (Throwable t) { return new Result(t, null); } break; // not yet // case ResultConstants.COMMIT_AND_CHAIN : // case ResultConstants.ROLLBACK_AND_CHAIN : } return emptyUpdateCount; } case ResultConstants.SQLSETCONNECTATTR : { switch (cmd.getConnectionAttrType()) { case ResultConstants.SQL_ATTR_SAVEPOINT_NAME : try { savepoint(cmd.getMainString()); } catch (Throwable t) { return new Result(t, null); } // case ResultConstants.SQL_ATTR_AUTO_IPD // - always true // default: throw - case never happens } return emptyUpdateCount; } case ResultConstants.SQLDISCONNECT : { close();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -