run

suspend fun <V> run(timeout: Duration? = null, parentSpan: RequestSpan? = null, durability: Durability? = null, metadataCollection: Keyspace? = null, transactionLogic: suspend TransactionAttemptContext.() -> V): TransactionResult<V>

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 times out.

  • An exception is thrown, either inside the transaction library or by the supplied transaction logic, that cannot be handled.

The transaction logic {@link Consumer} is provided an {@link TransactionAttemptContext}, 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 TransactionFailedException will be thrown. Similarly, there cannot be a commit followed by a rollback, or vice versa - this will also raise a TransactionFailedException.

If the transaction logic does not perform an explicit commit or rollback, then a commit will be performed anyway.

Return

The value returned by the transaction logic, along with diagnostic information.

Parameters

durability

Durability level for this transaction, or null to use the cluster environment's default transaction durability. Must not be Durability.none or Durability.clientVerified, which are not compatible with transactions.

timeout

Time allowed for this transaction to complete, or null to use the cluster environment's default timeout duration.

metadataCollection

The location in Couchbase to store metadata this transaction, or null to use the cluster environment's default metadata collection.

Throws

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