## org.h2.engine
Class Session

    java.lang.Object
      org.h2.engine.Session

All Implemented Interfaces:
  java.io.Closeable, SessionInterface

    public class Session

extends java.lang.Object

A session represents an embedded database connection. When using the server mode, this object resides on the server side and communicates with a SessionRemote object on the client side.

Field Summary
`static int` `LOG_WRITTEN` This special log position means that the log entry has been written.
`protected java.util.ArrayList<java.lang.String>` `sessionState`
`protected boolean` `sessionStateChanged`

Constructor Summary
`Session(Database database, User user, int id)`

Method Summary
`void` `addLocalTempTable(Table table)` Add a local temporary table to this session.
`void` `addLocalTempTableConstraint(Constraint constraint)` Add a local temporary constraint to this session.
`void` `addLocalTempTableIndex(Index index)` Add a local temporary index to this session.
`void` `addLock(Table table)` Add a lock for the given table.
`void` `addLogPos(int logId, int pos)` Called when a log entry for this session is added.
`void` `addProcedure(Procedure procedure)` Add a procedure to this session.
`void` `addSavepoint(java.lang.String name)` Create a savepoint that is linked to the current log position.
`void` `addTemporaryResult(ResultInterface result)` Remember the result set and close it as soon as the transaction is committed (if it needs to be closed).
`void` `afterWriting()` Called after writing has ended.
`void` `begin()` Begin a transaction.
`void` `cancel()` Cancel the current or next command (called when closing a connection).
`void` `checkCanceled()` Check if the current transaction is canceled by calling Statement.cancel() or because a session timeout was set and expired.
`void` `close()` Roll back pending transactions and close the session.
`void` `closeTemporaryResults()` Close all temporary result set.
`void` `commit(boolean ddl)` Commit the current transaction.
`JdbcConnection` `createConnection(boolean columnList)` Create an internal connection.
`Table` `findLocalTempTable(java.lang.String name)` Get the local temporary table if one exists with that name, or null if not.
`Constraint` `findLocalTempTableConstraint(java.lang.String name)` Get the local temporary constraint if one exists with that name, or null if not.
`Index` `findLocalTempTableIndex(java.lang.String name)` Get the local temporary index if one exists with that name, or null if not.
`boolean` `getAllowLiterals()`
`boolean` `getAutoCommit()` Check if this session is in auto-commit mode.
`long` `getCancel()` Get the cancel time.
`Command` `getCurrentCommand()`
`long` `getCurrentCommandStart()`
`java.lang.String` `getCurrentSchemaName()`
`Database` `getDatabase()`
`DataHandler` `getDataHandler()` Get the data handler object.
`int` `getFirstUncommittedLog()`
`int` `getId()`
`Value` `getLastIdentity()`
`java.util.HashMap<java.lang.String,Constraint>` `getLocalTempTableConstraints()` Get the map of constraints for all constraints on local, temporary tables, if any.
`java.util.HashMap<java.lang.String,Index>` `getLocalTempTableIndexes()`
`java.util.ArrayList<Table>` `getLocalTempTables()`
`Table[]` `getLocks()`
`int` `getLockTimeout()`
`int` `getModificationId()`
`java.lang.String` `getNextSystemIdentifier(java.lang.String sql)` Get the next system generated identifiers.
`int` `getPowerOffCount()` Get the number of disk operations before power failure is simulated.
`Procedure` `getProcedure(java.lang.String name)` Get the procedure with the given name, or null if none exists.
`int` `getQueryTimeout()`
`java.util.Random` `getRandom()`
`java.lang.String[]` `getSchemaSearchPath()`
`Value` `getScopeIdentity()`
`long` `getSessionStart()`
`Trace` `getTrace()` Get the trace object
`Value` `getTransactionId()`
`long` `getTransactionStart()`
`int` `getUndoLogPos()` Get the undo log position.
`User` `getUser()`
`Value` `getVariable(java.lang.String name)` Get the value of the specified user defined variable.
`java.lang.String[]` `getVariableNames()` Get the list of variable names that are set for this session.
`Table` `getWaitForLock()`
`int` `hashCode()`
`boolean` `isClosed()` Check if close was called.
`boolean` `isReconnectNeeded(boolean write)` Check if the database changed and therefore reconnecting is required.
`boolean` `isRedoLogBinaryEnabled()`
`boolean` `isUndoLogEnabled()`
`void` `log(Table table, short operation, Row row)` Add an undo log entry to this session.
`int` `nextObjectId()` Get the next object id.
`Prepared` `prepare(java.lang.String sql)` Parse and prepare the given SQL statement.
`Prepared` `prepare(java.lang.String sql, boolean rightsChecked)` Parse and prepare the given SQL statement.
`CommandInterface` `prepareCommand(java.lang.String sql, int fetchSize)` Parse a command and prepare it for execution.
`void` `prepareCommit(java.lang.String transactionName)` Prepare the given transaction.
`Command` `prepareLocal(java.lang.String sql)` Parse and prepare the given SQL statement.
`void` `readSessionState()` Read the session state if necessary.
`SessionInterface` `reconnect(boolean write)` Close the connection and open a new connection.
`protected void` `recreateSessionState()` Re-create the session state using the stored sessionState list.
`void` `removeLocalTempTable(Table table)` Drop and remove the given local temporary table from this session.
`void` `removeLocalTempTableIndex(Index index)` Drop and remove the given local temporary index from this session.
`void` `removeProcedure(java.lang.String name)` Remove a procedure from this session.
`void` `rollback()` Fully roll back the current transaction.
`void` `rollbackTo(int index, boolean trimToSize)` Partially roll back the current transaction.
`void` `rollbackToSavepoint(java.lang.String name)` Undo all operations back to the log position of the given savepoint.
`void` `setAllowLiterals(boolean b)`
`void` `setAutoCommit(boolean b)` Set the auto-commit mode.
`boolean` `setCommitOrRollbackDisabled(boolean x)`
`void` `setConnectionInfo(ConnectionInfo ci)`
`void` `setCurrentCommand(Command command)` Set the current command of this session.
`void` `setCurrentSchema(Schema schema)`
`void` `setLastIdentity(Value last)`
`void` `setLockTimeout(int lockTimeout)`
`void` `setPowerOffCount(int count)` Set the number of disk operations before power failure is simulated.
`void` `setPreparedTransaction(java.lang.String transactionName, boolean commit)` Commit or roll back the given transaction.
`void` `setQueryTimeout(int queryTimeout)`
`void` `setRedoLogBinary(boolean b)`
`void` `setSchemaSearchPath(java.lang.String[] schemas)`
`void` `setScopeIdentity(Value scopeIdentity)`
`void` `setThrottle(int throttle)`
`void` `setUndoLogEnabled(boolean b)`
`void` `setVariable(java.lang.String name, Value value)` Set the value of the given variable for this session.
`void` `setWaitForLock(Table table)`
`void` `throttle()` Wait for some time if this session is throttled (slowed down).
`java.lang.String` `toString()`
`void` `unlinkAtCommit(Value v)` Remember that the given LOB value must be un-linked (disconnected from the table) at commit.
`void` `unlinkAtCommitStop(Value v)` Do not unlink this LOB value at commit any longer.
`void` `unlockReadLocks()` Unlock all read locks.
`void` `waitIfExclusiveModeEnabled()` Wait if the exclusive mode has been enabled for another session.

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, notify, notifyAll, wait, wait, wait`

Field Detail

### LOG_WRITTEN

    public static final int LOG_WRITTEN

  This special log position means that the log entry has been written.

  See Also:
    Constant Field Values

### sessionState

    protected java.util.ArrayList<java.lang.String> sessionState

### sessionStateChanged

    protected boolean sessionStateChanged

Constructor Detail

### Session

    public Session(Database database,
                   User user,
                   int id)

Method Detail

### setCommitOrRollbackDisabled

    public boolean setCommitOrRollbackDisabled(boolean x)

### setVariable

    public void setVariable(java.lang.String name,
                            Value value)

  Set the value of the given variable for this session.

  Parameters:
    `name` - the name of the variable (may not be null)
    `value` - the new value (may not be null)

### getVariable

    public Value getVariable(java.lang.String name)

  Get the value of the specified user defined variable. This method always returns a value; it returns ValueNull.INSTANCE if the variable doesn't exist.

  Parameters:
    `name` - the variable name
  Returns:
    the value, or NULL

### getVariableNames

    public java.lang.String[] getVariableNames()

  Get the list of variable names that are set for this session.

  Returns:
    the list of names

### findLocalTempTable

    public Table findLocalTempTable(java.lang.String name)

  Get the local temporary table if one exists with that name, or null if not.

  Parameters:
    `name` - the table name
  Returns:
    the table, or null

### getLocalTempTables

    public java.util.ArrayList<Table> getLocalTempTables()

### addLocalTempTable

    public void addLocalTempTable(Table table)

  Add a local temporary table to this session.

  Parameters:
    `table` - the table to add
  Throws:
    `DbException` - if a table with this name already exists

### removeLocalTempTable

    public void removeLocalTempTable(Table table)

  Drop and remove the given local temporary table from this session.

  Parameters:
    `table` - the table

### findLocalTempTableIndex

    public Index findLocalTempTableIndex(java.lang.String name)

  Get the local temporary index if one exists with that name, or null if not.

  Parameters:
    `name` - the table name
  Returns:
    the table, or null

### getLocalTempTableIndexes

    public java.util.HashMap<java.lang.String,Index> getLocalTempTableIndexes()

### addLocalTempTableIndex

    public void addLocalTempTableIndex(Index index)

  Add a local temporary index to this session.

  Parameters:
    `index` - the index to add
  Throws:
    `DbException` - if a index with this name already exists

### removeLocalTempTableIndex

    public void removeLocalTempTableIndex(Index index)

  Drop and remove the given local temporary index from this session.

  Parameters:
    `index` - the index

### findLocalTempTableConstraint

    public Constraint findLocalTempTableConstraint(java.lang.String name)

  Get the local temporary constraint if one exists with that name, or null if not.

  Parameters:
    `name` - the constraint name
  Returns:
    the constraint, or null

### getLocalTempTableConstraints

    public java.util.HashMap<java.lang.String,Constraint> getLocalTempTableConstraints()

  Get the map of constraints for all constraints on local, temporary tables, if any. The map's keys are the constraints' names.

  Returns:
    the map of constraints, or null

### addLocalTempTableConstraint

    public void addLocalTempTableConstraint(Constraint constraint)

  Add a local temporary constraint to this session.

  Parameters:
    `constraint` - the constraint to add
  Throws:
    `DbException` - if a constraint with the same name already exists

### getAutoCommit

    public boolean getAutoCommit()

  Description copied from interface: `SessionInterface`
  Check if this session is in auto-commit mode.

  Returns:
    true if the session is in auto-commit mode

### getUser

    public User getUser()

### setAutoCommit

    public void setAutoCommit(boolean b)

  Description copied from interface: `SessionInterface`
  Set the auto-commit mode. This call doesn't commit the current transaction.

  Parameters:
    `b` - the new value

### getLockTimeout

    public int getLockTimeout()

### setLockTimeout

    public void setLockTimeout(int lockTimeout)

### prepareCommand

    public CommandInterface prepareCommand(java.lang.String sql,
                                           int fetchSize)

  Description copied from interface: `SessionInterface`
  Parse a command and prepare it for execution.

  Parameters:
    `sql` - the SQL statement
    `fetchSize` - the number of rows to fetch in one step
  Returns:
    the prepared command

### prepare

    public Prepared prepare(java.lang.String sql)

  Parse and prepare the given SQL statement. This method also checks the rights.

  Parameters:
    `sql` - the SQL statement
  Returns:
    the prepared statement

### prepare

    public Prepared prepare(java.lang.String sql,
                            boolean rightsChecked)

  Parse and prepare the given SQL statement.

  Parameters:
    `sql` - the SQL statement
    `rightsChecked` - true if the rights have already been checked
  Returns:
    the prepared statement

### prepareLocal

    public Command prepareLocal(java.lang.String sql)

  Parse and prepare the given SQL statement. This method also checks if the connection has been closed.

  Parameters:
    `sql` - the SQL statement
  Returns:
    the prepared statement

### getDatabase

    public Database getDatabase()

### getPowerOffCount

    public int getPowerOffCount()

  Description copied from interface: `SessionInterface`
  Get the number of disk operations before power failure is simulated. This is used for testing. If not set, 0 is returned

  Returns:
    the number of operations, or 0

### setPowerOffCount

    public void setPowerOffCount(int count)

  Description copied from interface: `SessionInterface`
  Set the number of disk operations before power failure is simulated. To disable the countdown, use 0.

  Parameters:
    `count` - the number of operations

### commit

    public void commit(boolean ddl)

  Commit the current transaction. If the statement was not a data definition statement, and if there are temporary tables that should be dropped or truncated at commit, this is done as well.

  Parameters:
    `ddl` - if the statement was a data definition statement

### rollback

    public void rollback()

  Fully roll back the current transaction.

### rollbackTo

    public void rollbackTo(int index,
                           boolean trimToSize)

  Partially roll back the current transaction.

  Parameters:
    `index` - the position to which should be rolled back
    `trimToSize` - if the list should be trimmed

### getUndoLogPos

    public int getUndoLogPos()

  Description copied from interface: `SessionInterface`
  Get the undo log position.

  Returns:
    the position (0 means no pending transaction)

### getId

    public int getId()

### cancel

    public void cancel()

  Description copied from interface: `SessionInterface`
  Cancel the current or next command (called when closing a connection).

### close

    public void close()

  Description copied from interface: `SessionInterface`
  Roll back pending transactions and close the session.

### addLock

    public void addLock(Table table)

  Add a lock for the given table. The object is unlocked on commit or rollback.

  Parameters:
    `table` - the table that is locked

### log

    public void log(Table table,
                    short operation,
                    Row row)

  Add an undo log entry to this session.

  Parameters:
    `table` - the table
    `operation` - the operation type (see `UndoLogRecord`)
    `row` - the row

### unlockReadLocks

    public void unlockReadLocks()

  Unlock all read locks. This is done if the transaction isolation mode is READ_COMMITTED.

### getRandom

    public java.util.Random getRandom()

### getTrace

    public Trace getTrace()

  Description copied from interface: `SessionInterface`
  Get the trace object

  Returns:
    the trace object

### setLastIdentity

    public void setLastIdentity(Value last)

### getLastIdentity

    public Value getLastIdentity()

### addLogPos

    public void addLogPos(int logId,
                          int pos)

  Called when a log entry for this session is added. The session keeps track of the first entry in the transaction log that is not yet committed.

  Parameters:
    `logId` - the transaction log id
    `pos` - the position of the log entry in the transaction log

### getFirstUncommittedLog

    public int getFirstUncommittedLog()

### addSavepoint

    public void addSavepoint(java.lang.String name)

  Create a savepoint that is linked to the current log position.

  Parameters:
    `name` - the savepoint name

### rollbackToSavepoint

    public void rollbackToSavepoint(java.lang.String name)

  Undo all operations back to the log position of the given savepoint.

  Parameters:
    `name` - the savepoint name

### prepareCommit

    public void prepareCommit(java.lang.String transactionName)

  Prepare the given transaction.

  Parameters:
    `transactionName` - the name of the transaction

### setPreparedTransaction

    public void setPreparedTransaction(java.lang.String transactionName,
                                       boolean commit)

  Commit or roll back the given transaction.

  Parameters:
    `transactionName` - the name of the transaction
    `commit` - true for commit, false for rollback

### isClosed

    public boolean isClosed()

  Description copied from interface: `SessionInterface`
  Check if close was called.

  Returns:
    if the session has been closed

### setThrottle

    public void setThrottle(int throttle)

### throttle

    public void throttle()

  Wait for some time if this session is throttled (slowed down).

### setCurrentCommand

    public void setCurrentCommand(Command command)

  Set the current command of this session. This is done just before executing the statement.

  Parameters:
    `command` - the command

### checkCanceled

    public void checkCanceled()

  Check if the current transaction is canceled by calling Statement.cancel() or because a session timeout was set and expired.

  Throws:
    `DbException` - if the transaction is canceled

### getCancel

    public long getCancel()

  Get the cancel time.

  Returns:
    the time or 0 if not set

### getCurrentCommand

    public Command getCurrentCommand()

### getCurrentCommandStart

    public long getCurrentCommandStart()

### getAllowLiterals

    public boolean getAllowLiterals()

### setAllowLiterals

    public void setAllowLiterals(boolean b)

### setCurrentSchema

    public void setCurrentSchema(Schema schema)

### getCurrentSchemaName

    public java.lang.String getCurrentSchemaName()

### createConnection

    public JdbcConnection createConnection(boolean columnList)

  Create an internal connection. This connection is used when initializing triggers, and when calling user defined functions.

  Parameters:
    `columnList` - if the url should be 'jdbc:columnlist:connection'
  Returns:
    the internal connection

### getDataHandler

    public DataHandler getDataHandler()

  Description copied from interface: `SessionInterface`
  Get the data handler object.

  Returns:
    the data handler

### unlinkAtCommit

    public void unlinkAtCommit(Value v)

  Remember that the given LOB value must be un-linked (disconnected from the table) at commit.

  Parameters:
    `v` - the value

### unlinkAtCommitStop

    public void unlinkAtCommitStop(Value v)

  Do not unlink this LOB value at commit any longer.

  Parameters:
    `v` - the value

### getNextSystemIdentifier

    public java.lang.String getNextSystemIdentifier(java.lang.String sql)

  Get the next system generated identifiers. The identifier returned does not occur within the given SQL statement.

  Parameters:
    `sql` - the SQL statement
  Returns:
    the new identifier

### addProcedure

    public void addProcedure(Procedure procedure)

  Add a procedure to this session.

  Parameters:
    `procedure` - the procedure to add

### removeProcedure

    public void removeProcedure(java.lang.String name)

  Remove a procedure from this session.

  Parameters:
    `name` - the name of the procedure to remove

### getProcedure

    public Procedure getProcedure(java.lang.String name)

  Get the procedure with the given name, or null if none exists.

  Parameters:
    `name` - the procedure name
  Returns:
    the procedure or null

### setSchemaSearchPath

    public void setSchemaSearchPath(java.lang.String[] schemas)

### getSchemaSearchPath

    public java.lang.String[] getSchemaSearchPath()

### hashCode

    public int hashCode()

  Overrides:
    `hashCode` in class `java.lang.Object`

### toString

    public java.lang.String toString()

  Overrides:
    `toString` in class `java.lang.Object`

### setUndoLogEnabled

    public void setUndoLogEnabled(boolean b)

### setRedoLogBinary

    public void setRedoLogBinary(boolean b)

### isUndoLogEnabled

    public boolean isUndoLogEnabled()

### begin

    public void begin()

  Begin a transaction.

### getSessionStart

    public long getSessionStart()

### getTransactionStart

    public long getTransactionStart()

### getLocks

    public Table[] getLocks()

### waitIfExclusiveModeEnabled

    public void waitIfExclusiveModeEnabled()

  Wait if the exclusive mode has been enabled for another session. This method returns as soon as the exclusive mode has been disabled.

### addTemporaryResult

    public void addTemporaryResult(ResultInterface result)

  Remember the result set and close it as soon as the transaction is committed (if it needs to be closed). This is done to delete temporary files as soon as possible, and free object ids of temporary tables.

  Parameters:
    `result` - the temporary result set

### closeTemporaryResults

    public void closeTemporaryResults()

  Close all temporary result set. This also deletes all temporary files held by the result sets.

### setQueryTimeout

    public void setQueryTimeout(int queryTimeout)

### getQueryTimeout

    public int getQueryTimeout()

### setWaitForLock

    public void setWaitForLock(Table table)

### getWaitForLock

    public Table getWaitForLock()

### getModificationId

    public int getModificationId()

### isReconnectNeeded

    public boolean isReconnectNeeded(boolean write)

  Description copied from interface: `SessionInterface`
  Check if the database changed and therefore reconnecting is required.

  Parameters:
    `write` - if the next operation may be writing
  Returns:
    true if reconnecting is required

### afterWriting

    public void afterWriting()

  Description copied from interface: `SessionInterface`
  Called after writing has ended. It needs to be called after isReconnectNeeded(true) returned false.

### reconnect

    public SessionInterface reconnect(boolean write)

  Description copied from interface: `SessionInterface`
  Close the connection and open a new connection.

  Parameters:
    `write` - if the next operation may be writing
  Returns:
    the new connection

### setConnectionInfo

    public void setConnectionInfo(ConnectionInfo ci)

### getTransactionId

    public Value getTransactionId()

### nextObjectId

    public int nextObjectId()

  Get the next object id.

  Returns:
    the next object id

### setScopeIdentity

    public void setScopeIdentity(Value scopeIdentity)

### getScopeIdentity

    public Value getScopeIdentity()

### isRedoLogBinaryEnabled

    public boolean isRedoLogBinaryEnabled()

### recreateSessionState

    protected void recreateSessionState()

  Re-create the session state using the stored sessionState list.

### readSessionState

    public void readSessionState()

  Read the session state if necessary.