You are developing a C# service that processes real-time bids for an online auction platform using the Azure Cosmos DB .NET SDK v3. The target container uses `/auctionId` as the partition key. To ensure data consistency, when a new bid is placed, you must atomically create a new bid item of type `Bid` and replace the existing auction summary item of type `AuctionSummary` within the same partition.
Which two of the following code segments must you use to successfully initialize and execute the transactional batch?
- TransactionalBatch batch = container.CreateTransactionalBatch(new PartitionKey(auctionId));Answer
- using TransactionalBatchResponse response = await batch.ExecuteAsync();Answer
- CTransactionalBatch batch = container.CreateTransactionalBatch();
- Dusing TransactionalBatchResponse response = await client.ExecuteAsync(batch);
Answer
Initialize the transactional batch using the container instance and the specific partition key, then execute the batch asynchronously using the ExecuteAsync method on the batch object.
To create and run a transaction using the Cosmos DB .NET SDK v3, you must first call CreateTransactionalBatch on the Container instance, passing the logical partition key (PartitionKey). Then, after adding the operations (such as CreateItem and ReplaceItem) to the batch, you execute the batch by calling the ExecuteAsync method directly on the TransactionalBatch object.
Step-by-Step Solution
Key Concept
Executing atomic transactions within a single logical partition in Azure Cosmos DB using the .NET SDK v3 TransactionalBatch.