public class TransactionRunner
extends java.lang.Object
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.
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.
Modifier and Type | Field | Description |
---|---|---|
static int |
DEFAULT_MAX_RETRIES |
The default maximum number of retries.
|
Constructor | Description |
---|---|
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.
|
Modifier and Type | Method | Description |
---|---|---|
boolean |
getAllowNestedTransactions() |
Returns whether nested transactions will be created if
run() is called when a transaction is already active for
the current thread. |
int |
getMaxRetries() |
Returns the maximum number of retries that will be performed when
deadlocks are detected.
|
TransactionConfig |
getTransactionConfig() |
Returns the transaction configuration used for calling
Environment.beginTransaction(com.sleepycat.db.Transaction, com.sleepycat.db.TransactionConfig) . |
int |
handleException(java.lang.Exception exception,
int retries,
int maxRetries) |
Handles exceptions that occur during a transaction, and may implement
transaction retry policy.
|
void |
run(TransactionWorker worker) |
Calls the
TransactionWorker.doWork() method and, for transactional
environments, may begin and end a transaction. |
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. |
void |
setMaxRetries(int maxRetries) |
Changes the maximum number of retries that will be performed when
deadlocks are detected.
|
void |
setTransactionConfig(TransactionConfig config) |
Changes the transaction configuration used for calling
Environment.beginTransaction(com.sleepycat.db.Transaction, com.sleepycat.db.TransactionConfig) . |
public static final int DEFAULT_MAX_RETRIES
public TransactionRunner(Environment env)
DEFAULT_MAX_RETRIES
) and
a null (default) TransactionConfig
will be used.env
- is the environment for running transactions.public TransactionRunner(Environment env, int maxRetries, TransactionConfig config)
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.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.public int getMaxRetries()
public void setMaxRetries(int maxRetries)
maxRetries
- the maximum number of retries.public boolean getAllowNestedTransactions()
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.
public void setAllowNestedTransactions(boolean allowNestedTxn)
run()
is called when a transaction is already active for
the current thread.
Calling this method does not impact transactions already running.allowNestedTxn
- whether nested transactions will be created.
Note that true may not be passed to this method in the JE product, since nested transactions are not supported by JE.
public TransactionConfig getTransactionConfig()
Environment.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.
public void setTransactionConfig(TransactionConfig config)
Environment.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.
config
- the transaction configuration.public void run(TransactionWorker worker) throws DatabaseException, java.lang.Exception
TransactionWorker.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.worker
- the TransactionWorker.DeadlockException
- when it is thrown by doWork() and the
maximum number of retries has occurred. The transaction will have been
aborted by this method.java.lang.Exception
- when any other exception is thrown by doWork(). The
exception will first be unwrapped by calling ExceptionUnwrapper.unwrap(java.lang.Exception)
. The transaction will have been aborted by
this method.DatabaseException
public int handleException(java.lang.Exception exception, int retries, int maxRetries) throws java.lang.Exception
run
method before calling this method.
The default implementation of this method throws the
exception
parameter if it is not an instance of DeadlockException
and otherwise returns the maxRetries
parameter value. This method can be overridden to throw a different
exception or return a different number of retries. For example:
Thread.sleep
for a short interval to
allow other transactions to finish.maxRetries
value
depending on the exception
that occurred.retries
value is greater or equal to the maxRetries
and
a DeadlockException
occurs, to override the default behavior
which is to throw the DeadlockException
.exception
- an exception that was thrown by the TransactionWorker.doWork()
method or thrown when beginning or committing
the transaction. If the retries
value is greater or equal to
maxRetries
when this method returns normally, this exception
will be thrown by the run
method.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 to getMaxRetries()
. This method may return a
different maximum retries value to override that default.maxRetries
parameter value
if the exception
parameter value is an instance of DeadlockException
.java.lang.Exception
- to cause the exception to be thrown by the run
method. The default policy is to throw the exception
parameter value if it is not an instance of DeadlockException
.Copyright (c) 1996, 2020 Oracle and/or its affiliates. All rights reserved.