Soru

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

You are developing a telemetry ingestion service that processes device events using the Azure Cosmos DB .NET SDK v3. The service uses a container configured with a partition key path of `/tenantId`.

You need to implement a helper method that performs two operations as a single transaction:
1. Create a new telemetry record of type `DeviceLog`.
2. Upsert a summary record of type `TenantSummary`.

Both records share the same `tenantId` value.

Which two of the following code segments should you use to complete the implementation?

  1. TransactionalBatch batch = container.CreateTransactionalBatch(new PartitionKey(tenantId))
    .CreateItem<DeviceLog>(log)
    .UpsertItem<TenantSummary>(summary);
    Cevap
  2. using (TransactionalBatchResponse response = await batch.ExecuteAsync())Cevap
  3. C
    TransactionalBatch batch = container.CreateTransactionalBatch()
    .CreateItem<DeviceLog>(log, new PartitionKey(tenantId))
    .UpsertItem<TenantSummary>(summary, new PartitionKey(tenantId));
  4. D
    using (TransactionalBatchResponse response = await batch.ExecuteAsync(new ItemRequestOptions
    {
    SessionToken = "tenant-session-token"
    }))

Cevap

The correct segments are the one that initializes the transactional batch by passing the partition key to the CreateTransactionalBatch method and fluently chains CreateItem and UpsertItem, and the one that executes the batch using await batch.ExecuteAsync() within a using block.
The correct segments are the one that initializes the batch with a partition key and chains the operations, and the one that executes the batch asynchronously in a using block. In the .NET SDK v3, a transactional batch is created on a container by passing the PartitionKey to CreateTransactionalBatch. Operations are chained without specifying partition keys because the entire batch is restricted to the same partition. The execution of the batch is asynchronous and returning a response that should be disposed.

Adım Adım Çözüm

1
Initialize the transactional batch on the container by passing the shared partition key.
A TransactionalBatch object is created and configured for the specific partition key value.
In SDK v3, transactions are scoped to a single partition key, which must be declared during batch creation.
2
Chain the desired item operations onto the transactional batch object without specifying the partition key for individual items.
The create and upsert operations are added to the transaction definition.
Fluent methods like CreateItem and UpsertItem on the batch do not accept a partition key parameter.
3
Execute the batch asynchronously using the ExecuteAsync method and manage its lifecycle with a using statement.
The batch executes atomically, and the TransactionalBatchResponse is properly disposed of.
The response object implements IDisposable and must be disposed to avoid resource leaks.

Anahtar Kavram

Transactional batch execution in Azure Cosmos DB .NET SDK v3 requires declaring the partition key at the batch initialization level and executing the batch asynchronously using the correct request options.
Bu soruyu puanla