Share via


TransactionalBatch Class

Definition

Represents a batch of operations against items with the same PartitionKey in a container that will be performed in a transactional manner at the Azure Cosmos DB service. Use CreateTransactionalBatch(PartitionKey) to create an instance of TransactionalBatch.

public abstract class TransactionalBatch
type TransactionalBatch = class
Public MustInherit Class TransactionalBatch
Inheritance
TransactionalBatch

Examples

This example atomically modifies a set of documents as a batch.

public class ToDoActivity
{
    public string type { get; set; }
    public string id { get; set; }
    public string status { get; set; }
}

string activityType = "personal";
ToDoActivity test1 = new ToDoActivity()
{
    type = activityType,
    id = "learning",
    status = "ToBeDone"
};

ToDoActivity test2 = new ToDoActivity()
{
    type = activityType,
    id = "shopping",
    status = "Done"
};

ToDoActivity test3 = new ToDoActivity()
{
    type = activityType,
    id = "swimming",
    status = "ToBeDone"
};

ToDoActivity test4 = new ToDoActivity()
{
    type = activityType,
    id = "running",
    status = "ToBeDone"
};

List<PatchOperation> patchOperations = new List<PatchOperation>();
patchOperations.Add(PatchOperation.Replace("/status", "InProgress");
patchOperations.Add(PatchOperation.Add("/progress", 50);

using (TransactionalBatchResponse batchResponse = await container.CreateTransactionalBatch(new Cosmos.PartitionKey(activityType))
    .CreateItem<ToDoActivity>(test1)
    .ReplaceItem<ToDoActivity>(test2.id, test2)
    .UpsertItem<ToDoActivity>(test3)
    .PatchItem(test4.id, patchOperations)
    .DeleteItem("reading")
    .CreateItemStream(streamPayload1)
    .ReplaceItemStream("eating", streamPayload2)
    .UpsertItemStream(streamPayload3)
    .ExecuteAsync())
{
   if (!batchResponse.IsSuccessStatusCode)
   {
       // Handle and log exception
       return;
   }

   // Look up interested results - eg. via typed access on operation results
   TransactionalBatchOperationResult<ToDoActivity> replaceResult = batchResponse.GetOperationResultAtIndex<ToDoActivity>(0);
   ToDoActivity readActivity = replaceResult.Resource;
}

This example atomically reads a set of documents as a batch.

string activityType = "personal";
using (TransactionalBatchResponse batchResponse = await container.CreateTransactionalBatch(new Cosmos.PartitionKey(activityType))
   .ReadItem("playing")
   .ReadItem("walking")
   .ReadItem("jogging")
   .ReadItem("running")
   .ExecuteAsync())
{
    // Look up interested results - eg. via direct access to operation result stream
    List<string> resultItems = new List<string>();
    foreach (TransactionalBatchOperationResult operationResult in batchResponse)
    {
        using (StreamReader streamReader = new StreamReader(operationResult.ResourceStream))
        {
            resultItems.Add(await streamReader.ReadToEndAsync());
        }
    }
}

Remarks

Limits on TransactionalBatch requests

Constructors

TransactionalBatch()

Methods

CreateItem<T>(T, TransactionalBatchItemRequestOptions)

Adds an operation to create an item into the batch.

CreateItemStream(Stream, TransactionalBatchItemRequestOptions)

Adds an operation to create an item into the batch.

DeleteItem(String, TransactionalBatchItemRequestOptions)

Adds an operation to delete an item into the batch.

ExecuteAsync(CancellationToken)

Executes the transactional batch at the Azure Cosmos service as an asynchronous operation.

ExecuteAsync(TransactionalBatchRequestOptions, CancellationToken)

Executes the transactional batch at the Azure Cosmos service as an asynchronous operation.

PatchItem(String, IReadOnlyList<PatchOperation>, TransactionalBatchPatchItemRequestOptions)

Adds an operation to patch an item into the batch.

ReadItem(String, TransactionalBatchItemRequestOptions)

Adds an operation to read an item into the batch.

ReplaceItem<T>(String, T, TransactionalBatchItemRequestOptions)

Adds an operation to replace an item into the batch.

ReplaceItemStream(String, Stream, TransactionalBatchItemRequestOptions)

Adds an operation to replace an item into the batch.

UpsertItem<T>(T, TransactionalBatchItemRequestOptions)

Adds an operation to upsert an item into the batch.

UpsertItemStream(Stream, TransactionalBatchItemRequestOptions)

Adds an operation to upsert an item into the batch.

Applies to