transactionBlocking

Executes multiple DQL queries within a single atomic transaction.

This ensures that either all statements are executed successfully, or none are executed at all, providing strong consistency guarantees. Certain mesh configurations may impose limitations on these guarantees. For more details, refer to the Ditto documentation.

Transactions are initiated as read-write transactions by default, and only a single read-write transaction is executed at any given time. Any other read-write transaction started concurrently will wait until the current transaction has been committed or rolled back. Therefore, it is crucial to ensure a transaction finishes as early as possible so that other read-write transactions aren’t blocked for a long time.

A transaction can also be configured to be read-only using the isReadonly parameter. Multiple read-only transactions can be executed concurrently. However, executing a mutating DQL statement in a read-only transaction will throw an error.

If errors occur in an execute() call within the transaction block and the error is caught and handled within the block, the transaction will continue to run and will not be rolled back. However, if an error is thrown at any point inside the transaction block or while committing the transaction, the transaction is implicitly rolled back, and the error is propagated up to the caller.

When a Ditto instance is explicitly closed or garbage collected, it will drive all pending transactions to completion before shutting down.

Warning: Calling ditto.store.execute() or creating a nested transaction within a transaction may lead to a deadlock.

For a complete guide on transactions, refer to the Ditto documentation.

Return

The transaction action that was taken (DittoTransactionCompletionAction.Commit or DittoTransactionCompletionAction.Rollback).

Since

4.11.0

Parameters

hint

A hint for the transaction, which is logged. Mostly useful for debugging and testing.

isReadonly

A flag indicating whether the transaction is read-only.

scope

A lambda that provides access to a DittoTransaction object, allowing DQL queries to be executed.

See also

Throws

DittoError.StoreError.TransactionReadOnly

if the transaction is read-only but a mutating query was executed.

for other errors.

, will rethrow any exception thrown within the scope lambda.


@JvmName(name = "transactionReturning")
fun <T> transactionBlocking(hint: String? = null, isReadonly: Boolean = false, scope: (DittoTransaction) -> T): T

Convenience method, same as transaction, but propagates the return value of the scope rather than the completion action.

The transaction is committed implicitly upon return and rolled back if an error is thrown.

Return

The value returned by the scope lambda.

Since

4.11.0

Parameters

hint

A hint for the transaction, which is logged. Mostly useful for debugging and testing.

isReadonly

A flag indicating whether the transaction is read-only.

scope

A suspend lambda that provides access to a DittoTransaction object, allowing DQL queries to be executed.

See also

Throws

DittoError.StoreError.TransactionReadOnly

if the transaction is read-only but a mutating query was executed.

for other errors.

, will rethrow any exception thrown within the scope lambda.