Soru

Zorluk: OrtaPerform Container and Item Operations in Azure Cosmos DB using SDK

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?

  1. TransactionalBatch batch = container.CreateTransactionalBatch(new PartitionKey(auctionId));Cevap
  2. using TransactionalBatchResponse response = await batch.ExecuteAsync();Cevap
  3. C
    TransactionalBatch batch = container.CreateTransactionalBatch();
  4. D
    using TransactionalBatchResponse response = await client.ExecuteAsync(batch);

Cevap

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.

Adım Adım Çözüm

1
Obtain a reference to the container and call CreateTransactionalBatch.
Create a TransactionalBatch object scoped to the target partition key.
Azure Cosmos DB transactional batches require all operations to reside in the same logical partition, so the partition key must be specified during batch initialization.
2
Add operations to the batch using methods like CreateItem and ReplaceItem.
Queue the operations to be executed atomically.
Operations are chained or added to the batch object prior to execution.
3
Execute the batch by calling ExecuteAsync on the TransactionalBatch instance.
Commit the transaction and receive a TransactionalBatchResponse.
The transaction is executed atomically in a single request to the Cosmos DB service.

Anahtar Kavram

Executing atomic transactions within a single logical partition in Azure Cosmos DB using the .NET SDK v3 TransactionalBatch.
Bu soruyu puanla