public interface Transaction extends Cancellable, Disposable, ReadTransaction, TransactionContext
Transaction is to use Database.run(com.foundationdb.async.Function). Otherwise, the client
must have retry logic for fatal failures, failures to commit, and other transient errors.Tuple API and
tuple layer documentation.TransactionContext, the methods run() and
runAsync() on a Transaction will simply attempt the operations
without any retry loop.commit() and wait on the result on all transactions, even
ones that only read. This is done automatically when using the retry loops from
Database.run(Function). This is because outstanding reads originating from a
Transaction will be cancelled when a Transaction is garbage collected.
Since the garbage collector reserves the right to collect an in-scope object if it
determines that there are no subsequent references it it, this can happen in seemingly
innocuous situations. Futures returned from commit() will block until
all reads are complete, thereby saving the calling code from this potentially confusing
situation.0xff are reserved for internal use.ROW_LIMIT_UNLIMITED| Modifier and Type | Method and Description |
|---|---|
void |
addReadConflictKey(byte[] key)
Adds a key to the transaction's read conflict ranges as if you had read
the key.
|
void |
addReadConflictRange(byte[] keyBegin,
byte[] keyEnd)
Adds a range of keys to the transaction's read conflict ranges as if you
had read the range.
|
void |
addWriteConflictKey(byte[] key)
Adds a key to the transaction's write conflict ranges as if you had
written the key.
|
void |
addWriteConflictRange(byte[] keyBegin,
byte[] keyEnd)
Adds a range of keys to the transaction's write conflict ranges as if you
had cleared the range.
|
void |
cancel()
Cancels the
Transaction. |
void |
clear(byte[] key)
Clears a given key from the database.
|
void |
clear(byte[] beginKey,
byte[] endKey)
Clears a range of keys in the database.
|
void |
clear(Range range)
Clears a range of keys in the database.
|
void |
clearRangeStartsWith(byte[] prefix)
Deprecated.
|
Future<Void> |
commit()
Commit this
Transaction. |
Long |
getCommittedVersion()
Gets the version number at which a successful commit modified the database.
|
Database |
getDatabase()
Returns the
Database that this Transaction is interacting
with. |
void |
mutate(MutationType optype,
byte[] key,
byte[] param)
An atomic operation is a single database command that carries out several
logical steps: reading the value of a key, performing a transformation on
that value, and writing the result.
|
PartialFuture<Void> |
onError(Exception e)
Convenience method for compatibility with code that does not want to do
type-based dispatching of errors.
|
Future<Void> |
onError(RuntimeException e)
Resets a transaction and returns a delayed signal for error recovery.
|
TransactionOptions |
options()
Returns a set of options that can be set on a
Transaction |
void |
reset()
Resets a
Transaction to its initial state after creation. |
<T> T |
run(Function<? super Transaction,T> retryable)
Run a function once against this
Transaction. |
<T> T |
run(PartialFunction<? super Transaction,T> retryable)
Run a function once against this
Transaction. |
<T> Future<T> |
runAsync(Function<? super Transaction,Future<T>> retryable)
Run a function once against this
Transaction. |
<T> PartialFuture<T> |
runAsync(PartialFunction<? super Transaction,? extends PartialFuture<T>> retryable)
Run a function once against this
Transaction. |
void |
set(byte[] key,
byte[] value)
Sets the value for a given key.
|
void |
setReadVersion(long version)
Directly sets the version of the database at which to execute reads.
|
ReadTransaction |
snapshot()
Return special-purpose, read-only view of the database.
|
Future<Void> |
watch(byte[] key)
Creates a watch that will become ready when it reports a change to
the value of the specified key.
A watch's behavior is relative to the transaction that created it. |
disposeget, getKey, getRange, getRange, getRange, getRange, getRange, getRange, getRange, getRange, getRange, getRange, getRange, getRange, getReadVersionread, read, readAsync, readAsyncReadTransaction snapshot()
void setReadVersion(long version)
past_version errors will be thrown from read operations.
Infrequently used.version - the version at which to read from the databasevoid addReadConflictRange(byte[] keyBegin,
byte[] keyEnd)
keyBegin - the first key in the range (inclusive)keyEnd - the ending key for the range (exclusive)void addReadConflictKey(byte[] key)
key - the key to be added to the rangevoid addWriteConflictRange(byte[] keyBegin,
byte[] keyEnd)
keyBegin - the first key in the range (inclusive)keyEnd - the ending key for the range (exclusive)void addWriteConflictKey(byte[] key)
key - the key to be added to the rangevoid set(byte[] key,
byte[] value)
commit() is called.key - the key whose value is to be setvalue - the value to set in the databaseIllegalArgumentExceptionFDBExceptionvoid clear(byte[] key)
commit() is called.key - the key whose value is to be clearedIllegalArgumentExceptionFDBExceptionvoid clear(byte[] beginKey,
byte[] endKey)
commit() is called.beginKey - the first clearendKey - the key one past the last key to clearIllegalArgumentExceptionFDBExceptionvoid clear(Range range)
commit() is called.range - the range of keys to clearFDBException@Deprecated void clearRangeStartsWith(byte[] prefix)
clear(Range) with a parameter from a call to
Range.startsWith(byte[]).prefix - the starting bytes from the keys to be cleared.FDBExceptionvoid mutate(MutationType optype, byte[] key, byte[] param)
MutationType is documented at its definition.optype - the operation to performkey - the target of the operationparam - the value with which to modify the keyTransactionOptions options()
TransactionTransactionFuture<Void> commit()
Transaction. See notes in class description. Consider using
Database's run() calls for managing
transactional access to FoundationDB.Future that, when set without error, guarantees the
Transaction's modifications committed durably to the
database. If the commit failed, it will throw an FDBException.
FDBException will be thrown with error code commit_unknown_result (1021).
The onError(java.lang.RuntimeException) function regards this exception as a retryable one, so
retry loops that don't specifically detect commit_unknown_result could end
up executing a transaction twice. For more information, see the FoundationDB
Developer Guide documentation.
If any operation is performed on a transaction after a commit has been
issued but before it has returned, both the commit and the operation will
throw an error code used_during_commit(2017). In this case, all
subsequent operations on this transaction will throw this error until
reset() is called.Long getCommittedVersion()
commit() on this Transaction, or the behavior is undefined.
Read-only transactions do not modify the database when committed and will have
a committed version of -1. Keep in mind that a transaction which reads keys and
then sets them to their current values may be optimized to a read-only transaction.Future<Void> onError(RuntimeException e)
Transaction could not be recovered from, the returned
Future will be set to an error state.e - the error caught while executing get()s and set()s on this TransactionFuture to be set as a signal for retrying the TransactionPartialFuture<Void> onError(Exception e)
RuntimeException, this version of onError() simply returns a
PartialFuture set to the supplied error.e - the error caught while executing get()s and set()s on this TransactionFuture to be set as a signal for retrying the Transactionvoid reset()
Transaction to its initial state after creation. This clears all
information about mutations, conflict information built from gets, and the
read version. This does not clear any options set on this Transaction via
a TransactionOptions object.void cancel()
Transaction. All pending and any future uses of the
Transaction will throw an RuntimeException. This Transaction
can be used again after reset() is called.cancel in interface CancellableFuture<Void> watch(byte[] key) throws FDBException
TransactionOptions.setReadYourWritesDisable(), and an attempt to do
so will raise a watches_disabled exception.commit_unknown_result exception. If an uncommitted transaction is
reset or destroyed, then any watches it created will be set with the
transaction_cancelled exception.too_many_watches exception.
Because a watch outlives the transaction that creates it, any watch that is no
longer needed should be cancelled.commit() for
the watch to be registered with the database.key - the key to watch for changes in valueFuture that will become ready when the value changesFDBException - if too many watches have been created on this database. The
limit defaults to 10,000 and can be modified with a call to
DatabaseOptions.setMaxWatches(long).Database getDatabase()
Database that this Transaction is interacting
with.Database object<T> T run(Function<? super Transaction,T> retryable)
Transaction. This call blocks while
user code is executing, returning the result of that code on completion.run in interface TransactionContextretryable - the block of logic to execute against a Transaction
in this contextretryable<T> T run(PartialFunction<? super Transaction,T> retryable) throws Exception
Transaction. This call blocks while
user code is executing, returning the result of that code on completion.run in interface TransactionContextretryable - the block of logic to execute against a Transaction
in this contextretryableException - if an error is encountered during executionTransactionContext.run(Function)<T> Future<T> runAsync(Function<? super Transaction,Future<T>> retryable)
Transaction. This call returns
immediately with a Future handle to the result.runAsync in interface TransactionContextretryable - the block of logic to execute against a Transaction
in this contextFuture that will be set to the return value of retryable<T> PartialFuture<T> runAsync(PartialFunction<? super Transaction,? extends PartialFuture<T>> retryable)
Transaction. This call returns
immediately with a PartialFuture handle to the result. Use this
formulation of runAsync(Function) if user code throws a checked exception.runAsync in interface TransactionContextretryable - the block of logic to execute against a Transaction
in this contextPartialFuture that will be set to the return value of retryable