Class TransactionRunner
TransactionWorker.doWork(), 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 run 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.
- When the run() method is called in a transactional environment and no transaction is active for the current thread, a new transaction is started before calling doWork(). If DeadlockException is thrown by doWork(), the transaction will be aborted and the process will be repeated up to the maximum number of retries. If another exception is thrown by doWork() or the maximum number of retries has occurred, the transaction will be aborted and the exception will be rethrown by the run() method. If no exception is thrown by doWork(), the transaction will be committed. The run() method will not attempt to commit or abort a transaction if it has already been committed or aborted by doWork().
- When the run() method is called and a transaction is active for the current thread, and nested transactions are enabled, a nested transaction is started before calling doWork(). The transaction that is active when calling the run() method will become the parent of the nested transaction. The nested transaction will be committed or aborted by the run() method following the same rules described above. Note that nested transactions may not be enabled for the JE product, since JE does not support nested transactions.
- When the run() method is called in a non-transactional environment, the doWork() method is called without starting a transaction. The run() method will return without committing or aborting a transaction, and any exceptions thrown by the doWork() method will be thrown by the run() method.
- When the run() method is called and a transaction is active for the current thread and nested transactions are not enabled (the default) the same rules as above apply. All the operations performed by the doWork() method will be part of the currently active transaction.
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
FieldsModifier and TypeFieldDescriptionstatic final intThe default maximum number of retries. -
Constructor Summary
ConstructorsConstructorDescriptionCreates 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
Modifier and TypeMethodDescriptionbooleanReturns whether nested transactions will be created ifrun()is called when a transaction is already active for the current thread.intReturns the maximum number of retries that will be performed when deadlocks are detected.Returns the transaction configuration used for callingEnvironment.beginTransaction(com.sleepycat.db.Transaction, com.sleepycat.db.TransactionConfig).inthandleException(Exception exception, int retries, int maxRetries) Handles exceptions that occur during a transaction, and may implement transaction retry policy.voidrun(TransactionWorker worker) Calls theTransactionWorker.doWork()method and, for transactional environments, may begin and end a transaction.voidsetAllowNestedTransactions(boolean allowNestedTxn) Changes whether nested transactions will be created ifrun()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.voidChanges the transaction configuration used for callingEnvironment.beginTransaction(com.sleepycat.db.Transaction, com.sleepycat.db.TransactionConfig).
-
Field Details
-
DEFAULT_MAX_RETRIES
public static final int DEFAULT_MAX_RETRIESThe default maximum number of retries.- See Also:
-
-
Constructor Details
-
TransactionRunner
Creates a transaction runner for a given Berkeley DB environment. The default maximum number of retries (DEFAULT_MAX_RETRIES) and a null (default)TransactionConfigwill be used.- Parameters:
env- is the environment for running transactions.
-
TransactionRunner
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 callingEnvironment.beginTransaction(com.sleepycat.db.Transaction, com.sleepycat.db.TransactionConfig), or null to use the default configuration. The configuration object is not cloned, and any modifications to it will impact subsequent transactions.
-
-
Method Details
-
getMaxRetries
public int getMaxRetries()Returns the maximum number of retries that will be performed when deadlocks are detected. -
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. -
getAllowNestedTransactions
public boolean getAllowNestedTransactions()Returns whether nested transactions will be created ifrun()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.
-
setAllowNestedTransactions
public void setAllowNestedTransactions(boolean allowNestedTxn) Changes whether nested transactions will be created ifrun()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.
-
getTransactionConfig
Returns the transaction configuration used for callingEnvironment.beginTransaction(com.sleepycat.db.Transaction, com.sleepycat.db.TransactionConfig).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.
-
setTransactionConfig
Changes the transaction configuration used for callingEnvironment.beginTransaction(com.sleepycat.db.Transaction, com.sleepycat.db.TransactionConfig).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.
-
run
Calls theTransactionWorker.doWork()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 callingExceptionUnwrapper.unwrap(java.lang.Exception). The transaction will have been aborted by this method.DatabaseException
-
handleException
Handles exceptions that occur during a transaction, and may implement transaction retry policy. The transaction is aborted by therunmethod before calling this method.The default implementation of this method throws the
exceptionparameter if it is not an instance ofDeadlockExceptionand otherwise returns themaxRetriesparameter value. This method can be overridden to throw a different exception or return a different number of retries. For example:- This method could call
Thread.sleepfor a short interval to allow other transactions to finish. - This method could return a different
maxRetriesvalue depending on theexceptionthat occurred. - This method could throw an application-defined exception when the
retriesvalue is greater or equal to themaxRetriesand aDeadlockExceptionoccurs, to override the default behavior which is to throw theDeadlockException.
- Parameters:
exception- an exception that was thrown by theTransactionWorker.doWork()method or thrown when beginning or committing the transaction. If theretriesvalue is greater or equal tomaxRetrieswhen this method returns normally, this exception will be thrown by therunmethod.retries- the current value of a counter that starts out at zero and is incremented when each retry is performed.maxRetries- the maximum retries to be performed. By default, this value is set togetMaxRetries(). This method may return a different maximum retries value to override that default.- Returns:
- the maximum number of retries to perform. The
default policy is to return the
maxRetriesparameter value if theexceptionparameter value is an instance ofDeadlockException. - Throws:
Exception- to cause the exception to be thrown by therunmethod. The default policy is to throw theexceptionparameter value if it is not an instance ofDeadlockException.- Since:
- 3.4
- This method could call
-