Class Transactions
- java.lang.Object
-
- com.couchbase.client.java.transactions.Transactions
-
public class Transactions extends Object
The starting point for creating Couchbase transactions.The main methods to run transactions are
run(java.util.function.Consumer<com.couchbase.client.java.transactions.TransactionAttemptContext>, com.couchbase.client.java.transactions.config.TransactionOptions)andreactive.Certain configurations, including the default one, will create background resources including cleanup threads, so it is highly recommended that an application create and reuse just one Transactions object.
- Author:
- Graham Pople
-
-
Constructor Summary
Constructors Constructor Description Transactions(Core core, JsonSerializer serializer)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description TransactionResultrun(Consumer<TransactionAttemptContext> transactionLogic)Runs supplied transactional logic until success or failure.TransactionResultrun(Consumer<TransactionAttemptContext> transactionLogic, TransactionOptions options)Runs supplied transactional logic until success or failure.
-
-
-
Constructor Detail
-
Transactions
@Internal public Transactions(Core core, JsonSerializer serializer)
-
-
Method Detail
-
run
public TransactionResult run(Consumer<TransactionAttemptContext> transactionLogic, @Nullable TransactionOptions options)
Runs supplied transactional logic until success or failure.The supplied transactional logic will be run if necessary multiple times, until either:
- The transaction successfully commits
- The transactional logic requests an explicit rollback
- The transaction timesout.
- An exception is thrown, either inside the transaction library or by the supplied transaction logic, that cannot be handled.
The transaction logic
Consumeris provided anTransactionAttemptContext, which contains methods allowing it to read, mutate, insert and delete documents, as well as commit or rollback the transaction.If the transaction logic performs a commit or rollback it must be the last operation performed. Else a
TransactionFailedExceptionwill be thrown. Similarly, there cannot be a commit followed by a rollback, or vice versa - this will also raise aCoreTransactionFailedException.If the transaction logic does not perform an explicit commit or rollback, then a commit will be performed anyway.
- Parameters:
transactionLogic- the application's transaction logicoptions- the configuration to use for this transaction- Returns:
- there is no need to check the returned
CoreTransactionResult, as success is implied by the lack of a thrown exception. It contains information useful only for debugging and logging. - Throws:
TransactionFailedException- or a derived exception if the transaction fails to commit for any reason, possibly after multiple retries. The exception contains further details of the error
-
run
public TransactionResult run(Consumer<TransactionAttemptContext> transactionLogic)
Runs supplied transactional logic until success or failure. A convenience overload forrun(java.util.function.Consumer<com.couchbase.client.java.transactions.TransactionAttemptContext>, com.couchbase.client.java.transactions.config.TransactionOptions)that provides a defaultPerTransactionConfig
-
-