Question

Difficulty: Very hardPerform Container and Item Operations in Azure Cosmos DB using SDK

You are developing a .NET application using the Azure Cosmos DB .NET SDK v3 to manage configuration settings for smart electricity meters. The container uses `/meterId` as its partition key.

You need to write a method that updates a meter's configuration status (updating an existing item) and creates a status transition audit log entry (creating a new item) for the same meter. Both operations must succeed or fail together as a single atomic unit. You must also implement Optimistic Concurrency Control (OCC) for the status update to prevent overwriting concurrent updates.

You have the following partially completed method:

csharp
public async Task<bool> UpdateMeterStatusAndLogAsync(
Container container,
string meterId,
MeterStatus updatedStatus,
string expectedETag)
{
// Initialize the transactional batch
// Add the status update operation using OCC

// (Remaining operations to add the audit log and execute the batch are implemented elsewhere)
}

Which two code segments should you use to perform these actions? (Select two.)

  1. TransactionalBatch batch = container.CreateTransactionalBatch(new PartitionKey(meterId));Answer
  2. batch.ReplaceItem<MeterStatus>(meterId, updatedStatus, new TransactionalBatchItemRequestOptions { IfMatchEtag = expectedETag });Answer
  3. C
    TransactionalBatch batch = container.CreateTransactionalBatch(meterId);
  4. D
    batch.ReplaceItem<MeterStatus>(meterId, updatedStatus, new ItemRequestOptions { IfMatchEtag = expectedETag });
  5. E
    batch.ReplaceItem<MeterStatus>(updatedStatus, meterId, new PartitionKey(meterId), new TransactionalBatchItemRequestOptions { IfMatchEtag = expectedETag });

Answer

The transactional batch must be initialized by passing a PartitionKey object to container.CreateTransactionalBatch, and the update operation must be added using batch.ReplaceItem with TransactionalBatchItemRequestOptions specifying the ETag.
To create an atomic transaction across multiple documents in the same partition, you must use the TransactionalBatch class. This batch is initialized via the container using a PartitionKey struct (demonstrated in the correct initialization option). To perform an update with Optimistic Concurrency Control (OCC) within the batch, you append a ReplaceItem operation using TransactionalBatchItemRequestOptions containing the expected ETag. The signature of ReplaceItem on a TransactionalBatch demands the item ID as the first parameter and the item object as the second (demonstrated in the correct replace option).

Step-by-Step Solution

1
Initialize the Transactional Batch
Use container.CreateTransactionalBatch(new PartitionKey(meterId))
The Cosmos DB SDK v3 scopes all transactional batches to a single partition key. The CreateTransactionalBatch method requires an instance of the PartitionKey struct, not a raw string.
2
Configure Optimistic Concurrency Control
Instantiate TransactionalBatchItemRequestOptions and set IfMatchEtag
Transactional batch operations use TransactionalBatchItemRequestOptions to apply request configurations like ETags. ItemRequestOptions cannot be used inside a transactional batch.
3
Add the point replace operation to the batch
Call batch.ReplaceItem<MeterStatus>(meterId, updatedStatus, options)
Unlike Container.ReplaceItemAsync which takes the item first, TransactionalBatch.ReplaceItem requires the string ID as the first parameter, followed by the item. It also does not accept a partition key parameter because the batch scope is predefined.

Key Concept

Transactional batch operations and optimistic concurrency control using Cosmos DB .NET SDK v3
Rate this question