public interface Transaction extends java.lang.AutoCloseable, ReadTransaction, TransactionContext
Transaction
is to use Database.run(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. CompletableFuture
s 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.TransactionOptions.setUsedDuringCommitProtectionDisable()
option. This is because the Java bindings disallow use of Transaction
objects after onError(java.lang.Throwable)
is called.Transaction
objects must be closed
when no longer
in use in order to free any associated resources.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.
|
void |
close()
Close the
Transaction object and release any associated resources. |
java.util.concurrent.CompletableFuture<java.lang.Void> |
commit()
Commit this
Transaction . |
java.lang.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. |
java.util.concurrent.CompletableFuture<byte[]> |
getVersionstamp()
Returns a future which will contain the versionstamp which was used by any versionstamp
operations in this transaction.
|
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.
|
java.util.concurrent.CompletableFuture<Transaction> |
onError(java.lang.Throwable e)
Resets a transaction and returns a delayed signal for error recovery.
|
<T> T |
run(java.util.function.Function<? super Transaction,T> retryable)
Run a function once against this
Transaction . |
<T> java.util.concurrent.CompletableFuture<T> |
runAsync(java.util.function.Function<? super Transaction,? extends java.util.concurrent.CompletableFuture<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.
|
java.util.concurrent.CompletableFuture<java.lang.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. |
get, getKey, getRange, getRange, getRange, getRange, getRange, getRange, getRange, getRange, getRange, getRange, getRange, getRange, getReadVersion, options
getExecutor, read, readAsync
ReadTransaction snapshot()
Transaction
with relaxed isolation propertiesvoid 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 databasejava.lang.IllegalArgumentException
- if key
or value
is null
FDBException
- if the set operation otherwise failsvoid clear(byte[] key)
commit()
is called.key
- the key whose value is to be clearedjava.lang.IllegalArgumentException
- if key
is null
FDBException
- if clear operation otherwise failsvoid clear(byte[] beginKey, byte[] endKey)
commit()
is called.beginKey
- the first clearendKey
- the key one past the last key to clearjava.lang.IllegalArgumentException
- if beginKey
or endKey
is null
FDBException
- if the clear operation otherwise failsvoid clear(Range range)
commit()
is called.range
- the range of keys to clearFDBException
- if the clear operation fails@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.FDBException
- if the clear-range operation failsvoid 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 keyjava.util.concurrent.CompletableFuture<java.lang.Void> commit()
Transaction
. See notes in class description. Consider using
Database
's run()
calls for managing
transactional access to FoundationDB.
As with other client/server databases, in some failure scenarios a client may
be unable to determine whether a transaction succeeded. In these cases, an
FDBException
will be thrown with error code commit_unknown_result
(1021).
The onError(java.lang.Throwable)
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.
CompletableFuture
that, when set without error, guarantees the
Transaction
's modifications committed durably to the
database. If the commit failed, it will throw an FDBException
.java.lang.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.java.util.concurrent.CompletableFuture<byte[]> getVersionstamp()
commit()
on this Transaction
. Read-only
transactions do not modify the database when committed and will result in the future
completing with an error. 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.java.util.concurrent.CompletableFuture<Transaction> onError(java.lang.Throwable e)
Transaction
could not be recovered from, the returned
CompletableFuture
will be set to an error state.
The current Transaction
object will be invalidated by this call and will throw errors
when used. The newly reset Transaction
will be returned through the CompletableFuture
if the error was retryable.
If the error is not retryable, then no reset Transaction
is returned, leaving this
Transaction
permanently invalidated.e
- the error caught while executing get()s and set()s on this Transaction
CompletableFuture
to be set with a reset Transaction
object to retry the transactionvoid cancel()
Transaction
. All pending and any future uses of the
Transaction
will throw an RuntimeException
.java.util.concurrent.CompletableFuture<java.lang.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 via onError(java.lang.Throwable)
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 valueCompletableFuture
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(java.util.function.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 TransactionContext
T
- the return type of retryable
retryable
- the block of logic to execute against this Transaction
retryable
<T> java.util.concurrent.CompletableFuture<T> runAsync(java.util.function.Function<? super Transaction,? extends java.util.concurrent.CompletableFuture<T>> retryable)
Transaction
. This call returns
immediately with a CompletableFuture
handle to the result.runAsync
in interface TransactionContext
T
- the return type of retryable
retryable
- the block of logic to execute against this Transaction
CompletableFuture
that will be set to the return value of retryable
void close()
Transaction
object and release any associated resources. This must be called at
least once after the Transaction
object is no longer in use. This can be called multiple
times, but care should be taken that it is not in use in another thread at the time of the call.close
in interface java.lang.AutoCloseable