Table of Contents

Struct TransactWriteCommand

Namespace
DynaSharp
Assembly
DynaSharp.dll

A TransactWriteItems between its actions: up to 100 puts, updates, deletes and condition checks, on any tables, that take effect together or not at all. The verbs are in TransactWriteExtensions, and each returns a step with the clauses of its own action and the verbs of the next one.

public readonly ref struct TransactWriteCommand : ITransactWriteChain<TransactWriteCommand>, IRetryable<TransactWriteCommand>
Implements
Inherited Members
Extension Methods

Examples

TransactMarker emailTaken = default;
try
{
    await client.TransactWrite()
        .Unique(emails, new EmailClaim(customer.Email, customer.Id), out emailTaken).ReturnOldOnFailure()
        .Put(customers, customer).IfNotExists()
        .SendAsync(ct);
}
catch (TransactionCanceledException exception) when (exception.ReasonFor(emailTaken) == TransactionFailure.ConditionalCheckFailed)
{
    Console.WriteLine(exception.ItemFor(emailTaken)!.To<EmailClaim>().CustomerId);
}

Remarks

A step converts to this type, which uses the step up, and that is what makes a loop possible: TransactWriteCommand tx = client.TransactWrite(); foreach (var order in orders) { tx = tx.Put(table, order).IfNotExists(); } await tx.SendAsync(ct);. A local of this type holds the chain, as any step does, and is built up and sent without an await in between. This type has no clause of an action, so the conversion ends what can be said about the action before it. Sending a transaction that got no action throws InvalidOperationException, so a loop that may add nothing skips the send.

DynamoDB cancels the whole transaction, with a TransactionCanceledException, when a condition of any action fails or an item takes part in another transaction. A write in a transaction costs twice a standard write, for the prepare and the commit, and a transaction holds at most 4 MB, must fit one region and cannot touch one item twice.

A transaction is idempotent for 10 minutes: DynamoDB recognizes the ClientRequestToken, and answers a repeat with the outcome of the first, without doing it again. Each send generates one token and every retry of the client carries it, so a lost response is safe. To make a retry of your own safe too, across restarts for example, pass Token<TChain>(TChain, string). Using a token again within the 10 minutes with other actions fails with an IdempotentParameterMismatchException.

Telemetry has one span for the request, with all tables in aws.dynamodb.table_names. See GetCommand<T> for what a command is and what it cannot do.

Methods

MaxAttempts(int)

Sets the number of attempts of this call including the first, in place of MaxAttempts. A retry after a lost response repeats a write that may have been applied, so 1 makes an ADD count at most once, and a lost response then reports a failure after the write was applied.

public TransactWriteCommand MaxAttempts(int attempts)

Parameters

attempts int

The number of attempts.

Returns

TransactWriteCommand

The command, which replaces this one.

Exceptions

ArgumentOutOfRangeException

attempts is below 1.

ToRequestJson()

Renders the body this transaction would send, as DynamoDB JSON, without sending it. It ends the command, and is meant for tests and debugging. The text contains the items and a generated token that nothing ever sends.

public string ToRequestJson()

Returns

string

The request body.

Exceptions

InvalidOperationException

The command was already sent, extended or rendered, or it has no action, which DynamoDB would reject.