Update several entities atomically
Use IVoxTransactionRunner when several entity writes must commit in one provider-local transaction.
For the team workflow, see Change data together.
Use IVoxTransactionRunner when a business operation must create, update or delete several entities as one unit.
Resolve the runner and the normal IVoxEntityUpdater from dependency injection:
using CommerceMind.Vox.Abstractions.Entities;
using CommerceMind.Vox.Abstractions.Transactions;
public class MoveProduct(
IVoxTransactionRunner transactions,
IVoxEntityUpdater updater)
{
public Task ExecuteAsync(
string productId,
long productVersion,
string oldCategoryId,
long oldCategoryVersion,
string newCategoryId,
long newCategoryVersion,
CancellationToken cancellationToken)
{
return transactions.ExecuteAsync(async token =>
{
await updater.UpdateAsync<Product>(
productId,
productVersion,
new ProductActions.MoveToCategory { CategoryId = newCategoryId },
cancellationToken: token);
await updater.UpdateAsync<Category>(
oldCategoryId,
oldCategoryVersion,
new CategoryActions.RemoveProduct { ProductId = productId },
cancellationToken: token);
await updater.UpdateAsync<Category>(
newCategoryId,
newCategoryVersion,
new CategoryActions.AddProduct { ProductId = productId },
cancellationToken: token);
}, cancellationToken);
}
}
The example action names are application code rather than built-in Vox actions.
Guarantees
Within the transaction:
- An exception, validation error or optimistic version conflict rolls back every entity change.
- Reads through Vox stores don't use the active read cache scope, and see changes made earlier in the transaction. The scope is cleared when the transaction ends, see read cache scopes.
- The same entity may be changed several times; use the version returned by the previous action.
- Entity data, relations, history and Vox outbox records commit together.
- Each save settles its own calculations inside the transaction, so they read what it staged: save the entities a calculation reads, eg three variants with new prices, before the entity that reads them, eg their product, and its price range is calculated from the new prices. Calculations between independent entities aren't settled beyond that order: when two entities read each other without a declared target, eg B's adjustment from A's input and A's total from B's adjustment, A is committed with B's old adjustment and catches up in the background. Use a declared target when entities must settle together, see Settled saves.
- Search indexing, the calculations of other entities that read the saved ones, and entity events start after the commit.
- Nested calls to
ExecuteAsyncjoin the outer transaction and do not commit independently.
Do not run transaction operations concurrently with Task.WhenAll. A transaction is a sequential unit of work.
Vox does not automatically rerun the delegate after a conflict because application handlers are not assumed to be
safe to replay.
Storage boundary
Every entity read or written by the transaction must use the same authoritative storage provider. For example, a transaction can update several Entity Framework entity types, or several entity types in one authoritative Redis provider. It cannot combine an Entity Framework entity with a Redis-master entity.
Redis stores only transient entity types, eg carts, which are stored in Entity Framework storage when no Redis is registered. So a transaction that updates a cart and a product works on a developer's machine without Redis and fails in production with it. Keep the writes of transient entity types and ordinary ones in separate transactions.
Full-text search is unavailable inside a transaction because the external index cannot contain staged changes. Use the authoritative entity stores for transaction decisions.
Validation and events
Custom validators run before their entity change is persisted. They can read entities changed earlier in the transaction and can reject the complete transaction. Validators should check state rather than perform irreversible operations in external systems.
Action handlers may use IVoxEntityUpdater to make additional entity changes. Inside a transaction those calls join
it; outside one they commit on their own when the handler runs, before the action's save, see
Effects outside the entity.
Entity events and subscriptions are post-commit reactions. A rollback produces no events. Failures in an event consumer cannot roll back an already committed transaction, so external consumers should retain their existing idempotency and retry behavior.
Balances of Accumulators
Changes of Accumulators, eg a claim of a discount code or a stock hold, join the transaction like entity writes of the same provider, so a claim and the application's record of it commit or roll back together. A refused change is a result, not an exception, and leaves the transaction's other writes as they were. The balances a transaction changes stay locked until it ends, so keep a transaction that changes a busy balance, eg the total uses of a public code, short.