com.sleepycat.collections

Class TransactionRunner

public class TransactionRunner extends Object

Starts a transaction, calls TransactionWorker, and handles transaction retry and exceptions. To perform a transaction, the user implements the TransactionWorker interface and passes an instance of that class to the TransactionRunner method.

A single TransactionRunner instance may be used by any number of threads for any number of transactions.

The behavior of the run() method depends on whether the environment is transactional, whether nested transactions are enabled, and whether a transaction is already active.

In a transactional environment, the rules described above support nested calls to the run() method and guarantee that the outermost call will cause the transaction to be committed or aborted. This is true whether or not nested transactions are supported or enabled. Note that nested transactions are provided as an optimization for improving concurrency but do not change the meaning of the outermost transaction. Nested transactions are not currently supported by the JE product.

Field Summary
static intDEFAULT_MAX_RETRIES
The default maximum number of retries.
Constructor Summary
TransactionRunner(Environment env)
Creates a transaction runner for a given Berkeley DB environment.
TransactionRunner(Environment env, int maxRetries, TransactionConfig config)
Creates a transaction runner for a given Berkeley DB environment and with a given number of maximum retries.
Method Summary
booleangetAllowNestedTransactions()
Returns whether nested transactions will be created if run() is called when a transaction is already active for the current thread.
intgetMaxRetries()
Returns the maximum number of retries that will be performed when deadlocks are detected.
TransactionConfiggetTransactionConfig()
Returns the transaction configuration used for calling Environment.
voidrun(TransactionWorker worker)
Calls the TransactionWorker method and, for transactional environments, may begin and end a transaction.
voidsetAllowNestedTransactions(boolean allowNestedTxn)
Changes whether nested transactions will be created if run() is called when a transaction is already active for the current thread.
voidsetMaxRetries(int maxRetries)
Changes the maximum number of retries that will be performed when deadlocks are detected.
voidsetTransactionConfig(TransactionConfig config)
Changes the transaction configuration used for calling Environment.

Field Detail

DEFAULT_MAX_RETRIES

public static final int DEFAULT_MAX_RETRIES
The default maximum number of retries.

Constructor Detail

TransactionRunner

public TransactionRunner(Environment env)
Creates a transaction runner for a given Berkeley DB environment. The default maximum number of retries (DEFAULT_MAX_RETRIES) and a null (default) TransactionConfig will be used.

Parameters: env is the environment for running transactions.

TransactionRunner

public TransactionRunner(Environment env, int maxRetries, TransactionConfig config)
Creates a transaction runner for a given Berkeley DB environment and with a given number of maximum retries.

Parameters: env is the environment for running transactions. maxRetries is the maximum number of retries that will be performed when deadlocks are detected. config the transaction configuration used for calling Environment, or null to use the default configuration. The configuration object is not cloned, and any modifications to it will impact subsequent transactions.

Method Detail

getAllowNestedTransactions

public boolean getAllowNestedTransactions()
Returns whether nested transactions will be created if run() is called when a transaction is already active for the current thread. By default this property is false.

Note that this method always returns false in the JE product, since nested transactions are not supported by JE.

getMaxRetries

public int getMaxRetries()
Returns the maximum number of retries that will be performed when deadlocks are detected.

getTransactionConfig

public TransactionConfig getTransactionConfig()
Returns the transaction configuration used for calling Environment.

If this property is null, the default configuration is used. The configuration object is not cloned, and any modifications to it will impact subsequent transactions.

Returns: the transaction configuration.

run

public void run(TransactionWorker worker)
Calls the TransactionWorker method and, for transactional environments, may begin and end a transaction. If the environment given is non-transactional, a transaction will not be used but the doWork() method will still be called. See the class description for more information.

Throws: DeadlockException when it is thrown by doWork() and the maximum number of retries has occurred. The transaction will have been aborted by this method. Exception when any other exception is thrown by doWork(). The exception will first be unwrapped by calling ExceptionUnwrapper. The transaction will have been aborted by this method.

setAllowNestedTransactions

public void setAllowNestedTransactions(boolean allowNestedTxn)
Changes whether nested transactions will be created if run() is called when a transaction is already active for the current thread. Calling this method does not impact transactions already running.

Note that true may not be passed to this method in the JE product, since nested transactions are not supported by JE.

setMaxRetries

public void setMaxRetries(int maxRetries)
Changes the maximum number of retries that will be performed when deadlocks are detected. Calling this method does not impact transactions already running.

setTransactionConfig

public void setTransactionConfig(TransactionConfig config)
Changes the transaction configuration used for calling Environment.

If this property is null, the default configuration is used. The configuration object is not cloned, and any modifications to it will impact subsequent transactions.

Parameters: config the transaction configuration.