public class Transaction
extends java.lang.Object
A single Transaction may be used to protect operations for any number of Databases in a given environment. However, a single Transaction may not be used for operations in more than one distinct environment.
Transaction handles are free-threaded; transactions handles may be used
concurrently by multiple threads. Once the Transaction.abort
or Transaction.commit
method
is called, the handle may not be accessed again, regardless of the success
or failure of the method, with one exception: the abort
method may
be called any number of times to simplify error handling.
To obtain a transaction with default attributes:
Transaction txn = myEnvironment.beginTransaction(null, null);
To customize the attributes of a transaction:
TransactionConfig config = new TransactionConfig(); config.setReadUncommitted(true); Transaction txn = myEnvironment.beginTransaction(null, config);
Modifier and Type | Class and Description |
---|---|
static class |
Transaction.State
The current state of the transaction.
|
Modifier and Type | Method and Description |
---|---|
void |
abort()
Cause an abnormal termination of the transaction.
|
void |
commit()
End the transaction.
|
void |
commit(Durability durability)
End the transaction using the specified durability requirements.
|
void |
commitNoSync()
End the transaction, not writing to stable storage and not committing
synchronously.
|
void |
commitSync()
End the transaction, writing to stable storage and committing
synchronously.
|
void |
commitWriteNoSync()
End the transaction, writing to stable storage but not committing
synchronously.
|
CommitToken |
getCommitToken()
This method is intended for use with a replicated environment.
|
long |
getId()
Return the transaction's unique ID.
|
long |
getLockTimeout(java.util.concurrent.TimeUnit unit)
Returns the lock request timeout value for the transaction.
|
java.lang.String |
getName()
Get the user visible name for the transaction.
|
Transaction.State |
getState()
Returns the current state of the transaction.
|
long |
getTxnTimeout(java.util.concurrent.TimeUnit unit)
Returns the timeout value for the transaction lifetime.
|
boolean |
isValid()
Returns whether this
Transaction is open, which is equivalent
to when getState() returns Transaction.State.OPEN . |
void |
setLockTimeout(long timeOut)
Deprecated.
as of 4.0, replaced by
setLockTimeout(long,
TimeUnit) . |
void |
setLockTimeout(long timeOut,
java.util.concurrent.TimeUnit unit)
Configures the lock request timeout value for the transaction.
|
void |
setName(java.lang.String name)
Set the user visible name for the transaction.
|
void |
setTxnTimeout(long timeOut)
Deprecated.
as of 4.0, replaced by
setTxnTimeout(long,
TimeUnit) . |
void |
setTxnTimeout(long timeOut,
java.util.concurrent.TimeUnit unit)
Configures the timeout value for the transaction lifetime.
|
java.lang.String |
toString() |
public void abort() throws DatabaseException
The log is played backward, and any necessary undo operations are done. Before Transaction.abort returns, any locks held by the transaction will have been released.
In the case of nested transactions, aborting a parent transaction causes all children (unresolved or not) of the parent transaction to be aborted.
All cursors opened within the transaction must be closed before the transaction is aborted.
After this method has been called, regardless of its return, the
Transaction
handle may not be accessed again, with one
exception: the abort
method itself may be called any number of
times to simplify error handling.
WARNING: To guard against memory leaks, the application should discard all references to the closed handle. While BDB makes an effort to discard references from closed objects to the allocated memory for an environment, this behavior is not guaranteed. The safe course of action for an application is to discard all references to closed BDB objects.
EnvironmentFailureException
- if an unexpected, internal or
environment-wide failure occurs.java.lang.IllegalStateException
- if the environment has been closed, or
cursors associated with the transaction are still open.DatabaseException
public long getId()
public CommitToken getCommitToken() throws java.lang.IllegalStateException
It returns the commitToken associated with a successful replicated commit. A null value is returned if the txn was not associated with a replicated environment, or the txn did not result in any changes to the environment. This method should only be called after the transaction has finished.
This method is typically used in conjunction with the
CommitPointConsistencyPolicy
.
java.lang.IllegalStateException
- if the method is called before the
transaction has committed or aborted.CommitPointConsistencyPolicy
public void commit() throws DatabaseException
If the environment is not configured for synchronous commit, the commit will not necessarily have been committed to stable storage before the call returns. This means the transaction will exhibit the ACI (atomicity, consistency, and isolation) properties, but not D (durability); that is, database integrity will be maintained, but it is possible this transaction may be undone during recovery.
All cursors opened within the transaction must be closed before the transaction is committed.
If the method encounters an error, the transaction will have been aborted when the call returns.
After this method has been called, regardless of its return, the
Transaction
handle may not be accessed again, with one
exception: the abort
method may be called any number of times
to simplify error handling.
WARNING: To guard against memory leaks, the application should discard all references to the closed handle. While BDB makes an effort to discard references from closed objects to the allocated memory for an environment, this behavior is not guaranteed. The safe course of action for an application is to discard all references to closed BDB objects.
InsufficientReplicasException
- if the master
in a replicated environment could not contact a quorum of replicas as
determined by the Durability.ReplicaAckPolicy
.InsufficientAcksException
- if the master in
a replicated environment did not receive enough replica acknowledgments,
although the commit succeeded locally.ReplicaWriteException
- if a write operation
was performed with this transaction, but this node is now a Replica.OperationFailureException
- if this exception occurred earlier and
caused the transaction to be invalidated.EnvironmentFailureException
- if an unexpected, internal or
environment-wide failure occurs.java.lang.IllegalStateException
- if the transaction or environment has been
closed, or cursors associated with the transaction are still open.DatabaseException
public void commit(Durability durability) throws DatabaseException
Durability
for specific exceptions that could result when the
durability requirements cannot be satisfied.
All cursors opened within the transaction must be closed before the transaction is committed.
If the method encounters an error, the transaction will have been aborted when the call returns.
After this method has been called, regardless of its return, the
Transaction
handle may not be accessed again, with one
exception: the abort
method may be called any number of times
to simplify error handling.
WARNING: To guard against memory leaks, the application should discard all references to the closed handle. While BDB makes an effort to discard references from closed objects to the allocated memory for an environment, this behavior is not guaranteed. The safe course of action for an application is to discard all references to closed BDB objects.
durability
- the durability requirements for this transactionInsufficientReplicasException
- if the master
in a replicated environment could not contact enough replicas to
initiate the commit.InsufficientAcksException
- if the master in
a replicated environment did not receive enough replica acknowledgments,
althought the commit succeeded locally.ReplicaWriteException
- if a write operation
was performed with this transaction, but this node is now a Replica.OperationFailureException
- if this exception occurred earlier and
caused the transaction to be invalidated.EnvironmentFailureException
- if an unexpected, internal or
environment-wide failure occurs.java.lang.IllegalStateException
- if the transaction or environment has been
closed, or cursors associated with the transaction are still open.java.lang.IllegalArgumentException
- if an invalid parameter is specified.DatabaseException
public void commitSync() throws DatabaseException
This behavior is the default for database environments unless
otherwise configured using the EnvironmentConfig.setTxnNoSync
method. This behavior may also be set
for a single transaction using the Environment.beginTransaction
method. Any value specified to this
method overrides both of those settings.
All cursors opened within the transaction must be closed before the transaction is committed.
If the method encounters an error, the transaction will have been aborted when the call returns.
After this method has been called, regardless of its return, the
Transaction
handle may not be accessed again, with one
exception: the abort
method may be called any number of times
to simplify error handling.
WARNING: To guard against memory leaks, the application should discard all references to the closed handle. While BDB makes an effort to discard references from closed objects to the allocated memory for an environment, this behavior is not guaranteed. The safe course of action for an application is to discard all references to closed BDB objects.
InsufficientReplicasException
- if the master
in a replicated environment could not contact enough replicas to
initiate the commit.InsufficientAcksException
- if the master in
a replicated environment did not receive enough replica acknowledgments,
althought the commit succeeded locally.ReplicaWriteException
- if a write operation
was performed with this transaction, but this node is now a Replica.OperationFailureException
- if this exception occurred earlier and
caused the transaction to be invalidated.EnvironmentFailureException
- if an unexpected, internal or
environment-wide failure occurs.java.lang.IllegalStateException
- if the transaction or environment has been
closed, or cursors associated with the transaction are still open.DatabaseException
public void commitNoSync() throws DatabaseException
This behavior may be set for a database environment using the EnvironmentConfig.setTxnNoSync
method or for a single transaction using
the Environment.beginTransaction
method. Any value specified to this
method overrides both of those settings.
All cursors opened within the transaction must be closed before the transaction is committed.
If the method encounters an error, the transaction will have been aborted when the call returns.
After this method has been called, regardless of its return, the
Transaction
handle may not be accessed again, with one
exception: the abort
method may be called any number of times
to simplify error handling.
WARNING: To guard against memory leaks, the application should discard all references to the closed handle. While BDB makes an effort to discard references from closed objects to the allocated memory for an environment, this behavior is not guaranteed. The safe course of action for an application is to discard all references to closed BDB objects.
InsufficientReplicasException
- if the master
in a replicated environment could not contact enough replicas to
initiate the commit.InsufficientAcksException
- if the master in
a replicated environment did not receive enough replica acknowledgments,
althought the commit succeeded locally.ReplicaWriteException
- if a write operation
was performed with this transaction, but this node is now a Replica.OperationFailureException
- if this exception occurred earlier and
caused the transaction to be invalidated.EnvironmentFailureException
- if an unexpected, internal or
environment-wide failure occurs.java.lang.IllegalStateException
- if the transaction or environment has been
closed, or cursors associated with the transaction are still open.DatabaseException
public void commitWriteNoSync() throws DatabaseException
This behavior is the default for database environments unless
otherwise configured using the EnvironmentConfig.setTxnNoSync
method. This behavior may also be set
for a single transaction using the Environment.beginTransaction
method. Any value specified to this
method overrides both of those settings.
All cursors opened within the transaction must be closed before the transaction is committed.
If the method encounters an error, the transaction will have been aborted when the call returns.
After this method has been called, regardless of its return, the
Transaction
handle may not be accessed again, with one
exception: the abort
method may be called any number of times
to simplify error handling.
WARNING: To guard against memory leaks, the application should discard all references to the closed handle. While BDB makes an effort to discard references from closed objects to the allocated memory for an environment, this behavior is not guaranteed. The safe course of action for an application is to discard all references to closed BDB objects.
InsufficientReplicasException
- if the master
in a replicated environment could not contact enough replicas to
initiate the commit.InsufficientAcksException
- if the master in
a replicated environment did not receive enough replica acknowledgments,
althought the commit succeeded locally.ReplicaWriteException
- if a write operation
was performed with this transaction, but this node is now a Replica.OperationFailureException
- if this exception occurred earlier and
caused the transaction to be invalidated.EnvironmentFailureException
- if an unexpected, internal or
environment-wide failure occurs.java.lang.IllegalStateException
- if the transaction or environment has been
closed, or cursors associated with the transaction are still open.DatabaseException
public long getTxnTimeout(java.util.concurrent.TimeUnit unit) throws EnvironmentFailureException, java.lang.IllegalStateException, java.lang.IllegalArgumentException
If setTxnTimeout(long,TimeUnit)
has not been called to
configure the timeout, the environment configuration value (EnvironmentConfig.TXN_TIMEOUT
)is returned.
unit
- the TimeUnit
of the returned value. May not be null.EnvironmentFailureException
- if an unexpected, internal or
environment-wide failure occurs.java.lang.IllegalStateException
- if the transaction or environment has been
closed.java.lang.IllegalArgumentException
- if the unit is null.public void setTxnTimeout(long timeOut, java.util.concurrent.TimeUnit unit) throws java.lang.IllegalArgumentException, DatabaseException
If the transaction runs longer than this time, an operation using the
transaction may throw TransactionTimeoutException
. The
transaction timeout is checked when locking a record, as part of a read
or write operation.
A value of zero (which is the default) disables timeouts for the
transaction, meaning that no limit on the duration of the transaction is
enforced. Note that the setLockTimeout(long, TimeUnit)
lock
timeout} is independent of the transaction timeout, and the lock timeout
should not normally be set to zero.
timeOut
- The timeout value for the transaction lifetime, or zero
to disable transaction timeouts.unit
- the TimeUnit
of the timeOut value. May be null only
if timeOut is zero.EnvironmentFailureException
- if an unexpected, internal or
environment-wide failure occurs.java.lang.IllegalStateException
- if the transaction or environment has been
closed.java.lang.IllegalArgumentException
- if timeOut or unit is invalid.DatabaseException
public void setTxnTimeout(long timeOut) throws java.lang.IllegalArgumentException, DatabaseException
setTxnTimeout(long,
TimeUnit)
.setTxnTimeout(long, TimeUnit.MICROSECONDS);
java.lang.IllegalArgumentException
DatabaseException
public long getLockTimeout(java.util.concurrent.TimeUnit unit) throws EnvironmentFailureException, java.lang.IllegalStateException, java.lang.IllegalArgumentException
If setLockTimeout(long,TimeUnit)
has not been called to
configure the timeout, the environment configuration value (EnvironmentConfig.LOCK_TIMEOUT
) is returned.
unit
- the TimeUnit
of the returned value. May not be null.EnvironmentFailureException
- if an unexpected, internal or
environment-wide failure occurs.java.lang.IllegalStateException
- if the transaction or environment has been
closed.java.lang.IllegalArgumentException
- if the unit is null.public void setLockTimeout(long timeOut, java.util.concurrent.TimeUnit unit) throws java.lang.IllegalArgumentException, DatabaseException
default lock timeout
.
A value of zero disables lock timeouts. This is not recommended, even when the application expects that deadlocks will not occur or will be easily resolved. A lock timeout is a fall-back that guards against unexpected "live lock", unresponsive threads, or application failure to close a cursor or to commit or abort a transaction.
timeOut
- The lock timeout for all transactional and
non-transactional operations, or zero to disable lock timeouts.unit
- the TimeUnit
of the timeOut value. May be null only
if timeOut is zero.EnvironmentFailureException
- if an unexpected, internal or
environment-wide failure occurs.java.lang.IllegalStateException
- if the transaction or environment has been
closed.java.lang.IllegalArgumentException
- if timeOut or unit is invalid.DatabaseException
public void setLockTimeout(long timeOut) throws java.lang.IllegalArgumentException, DatabaseException
setLockTimeout(long,
TimeUnit)
.setLockTimeout(long, TimeUnit.MICROSECONDS);
java.lang.IllegalArgumentException
DatabaseException
public void setName(java.lang.String name)
name
- The user visible name for the transaction.public java.lang.String getName()
public java.lang.String toString()
toString
in class java.lang.Object
public boolean isValid()
Transaction
is open, which is equivalent
to when getState()
returns Transaction.State.OPEN
. See Transaction.State.OPEN
for more
information.
When an OperationFailureException
, or one of its subclasses,
is caught, the isValid
method may be called to determine whether
the Transaction
can continue to be used, or should be
aborted.
public Transaction.State getState()
Copyright (c) 2002, 2017 Oracle and/or its affiliates. All rights reserved.