com.sleepycat.collections
Class CurrentTransaction

java.lang.Object
  extended by com.sleepycat.collections.CurrentTransaction

public class CurrentTransaction
extends Object

Provides access to the current transaction for the current thread within the context of a Berkeley DB environment. This class provides explicit transaction control beyond that provided by the TransactionRunner class. However, both methods of transaction control manage per-thread transactions.

Author:
Mark Hayes

Nested Class Summary
(package private) static class CurrentTransaction.CdbCursors
           
 
Method Summary
 Transaction abortTransaction()
          Aborts the transaction that is active for the current thread for this environment and makes the parent transaction (if any) the current transaction.
 Transaction beginTransaction(TransactionConfig config)
          Begins a new transaction for this environment and associates it with the current thread.
(package private)  void closeCursor(Cursor cursor)
          Closes a cursor.
 Transaction commitTransaction()
          Commits the transaction that is active for the current thread for this environment and makes the parent transaction (if any) the current transaction.
(package private)  Cursor dupCursor(Cursor cursor, boolean writeCursor, boolean samePosition)
          Duplicates a cursor for a given database.
 Environment getEnvironment()
          Returns the underlying Berkeley DB environment.
static CurrentTransaction getInstance(Environment env)
          Gets the CurrentTransaction accessor for a specified Berkeley DB environment.
(package private) static CurrentTransaction getInstanceInternal(Environment env)
          Gets the CurrentTransaction accessor for a specified Berkeley DB environment.
 Transaction getTransaction()
          Returns the transaction associated with the current thread for this environment, or null if no transaction is active.
(package private)  LockMode getWriteLockMode()
          Return the LockMode.RMW or null, depending on whether locking is enabled.
(package private)  boolean isAutoCommitAllowed()
          Returns whether auto-commit may be performed by the collections API.
(package private)  boolean isCDBCursorOpen(Database db)
          Returns true if a CDB cursor is open and therefore a Database write operation should not be attempted since a self-deadlock may result.
(package private)  boolean isCdbMode()
          Returns whether this is a Concurrent Data Store environment.
(package private)  boolean isReadUncommitted()
          Returns whether the current transaction is a readUncommitted transaction.
(package private)  boolean isTxnMode()
          Returns whether this is a transactional environment.
(package private)  Cursor openCursor(Database db, CursorConfig cursorConfig, boolean writeCursor, Transaction txn)
          Opens a cursor for a given database, dup'ing an existing CDB cursor if one is open for the current thread.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getInstance

public static CurrentTransaction getInstance(Environment env)
Gets the CurrentTransaction accessor for a specified Berkeley DB environment. This method always returns the same reference when called more than once with the same environment parameter.

Parameters:
env - is an open Berkeley DB environment.
Returns:
the CurrentTransaction accessor for the given environment, or null if the environment is not transactional.

getInstanceInternal

static CurrentTransaction getInstanceInternal(Environment env)
Gets the CurrentTransaction accessor for a specified Berkeley DB environment. Unlike getInstance(), this method never returns null.

Parameters:
env - is an open Berkeley DB environment.

isTxnMode

final boolean isTxnMode()
Returns whether this is a transactional environment.


isCdbMode

final boolean isCdbMode()
Returns whether this is a Concurrent Data Store environment.


getWriteLockMode

final LockMode getWriteLockMode()
Return the LockMode.RMW or null, depending on whether locking is enabled. LockMode.RMW will cause an error if passed when locking is not enabled. Locking is enabled if locking or transactions were specified for this environment.


getEnvironment

public final Environment getEnvironment()
Returns the underlying Berkeley DB environment.


getTransaction

public final Transaction getTransaction()
Returns the transaction associated with the current thread for this environment, or null if no transaction is active.


isAutoCommitAllowed

boolean isAutoCommitAllowed()
                      throws DatabaseException
Returns whether auto-commit may be performed by the collections API. True is returned no collections API transaction is currently active, and no XA transaction is currently active.

Throws:
DatabaseException

beginTransaction

public final Transaction beginTransaction(TransactionConfig config)
                                   throws DatabaseException
Begins a new transaction for this environment and associates it with the current thread. If a transaction is already active for this environment and thread, a nested transaction will be created.

Parameters:
config - the transaction configuration used for calling Environment.beginTransaction(com.sleepycat.je.Transaction, com.sleepycat.je.TransactionConfig), or null to use the default configuration.
Returns:
the new transaction.
Throws:
DatabaseException - if the transaction cannot be started, in which case any existing transaction is not affected.
IllegalStateException - if a transaction is already active and nested transactions are not supported by the environment.

commitTransaction

public final Transaction commitTransaction()
                                    throws DatabaseException,
                                           IllegalStateException
Commits the transaction that is active for the current thread for this environment and makes the parent transaction (if any) the current transaction.

Returns:
the parent transaction or null if the committed transaction was not nested.
Throws:
DatabaseException - if an error occurs committing the transaction. The transaction will still be closed and the parent transaction will become the current transaction.
IllegalStateException - if no transaction is active for the current thread for this environment.

abortTransaction

public final Transaction abortTransaction()
                                   throws DatabaseException,
                                          IllegalStateException
Aborts the transaction that is active for the current thread for this environment and makes the parent transaction (if any) the current transaction.

Returns:
the parent transaction or null if the aborted transaction was not nested.
Throws:
DatabaseException - if an error occurs aborting the transaction. The transaction will still be closed and the parent transaction will become the current transaction.
IllegalStateException - if no transaction is active for the current thread for this environment.

isReadUncommitted

final boolean isReadUncommitted()
Returns whether the current transaction is a readUncommitted transaction.


openCursor

Cursor openCursor(Database db,
                  CursorConfig cursorConfig,
                  boolean writeCursor,
                  Transaction txn)
            throws DatabaseException
Opens a cursor for a given database, dup'ing an existing CDB cursor if one is open for the current thread.

Throws:
DatabaseException

dupCursor

Cursor dupCursor(Cursor cursor,
                 boolean writeCursor,
                 boolean samePosition)
           throws DatabaseException
Duplicates a cursor for a given database.

Parameters:
writeCursor - true to open a write cursor in a CDB environment, and ignored for other environments.
samePosition - is passed through to Cursor.dup().
Returns:
the open cursor.
Throws:
DatabaseException - if a database problem occurs.

closeCursor

void closeCursor(Cursor cursor)
           throws DatabaseException
Closes a cursor.

Parameters:
cursor - the cursor to close.
Throws:
DatabaseException - if a database problem occurs.

isCDBCursorOpen

boolean isCDBCursorOpen(Database db)
                  throws DatabaseException
Returns true if a CDB cursor is open and therefore a Database write operation should not be attempted since a self-deadlock may result.

Throws:
DatabaseException


Copyright 2004-2005 Sleepycat, Inc. All Rights Reserved.