Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[State CosmosDB] Transactions do not work #3398

Closed
koumdev opened this issue Apr 11, 2024 · 1 comment
Closed

[State CosmosDB] Transactions do not work #3398

koumdev opened this issue Apr 11, 2024 · 1 comment
Labels
documentation Improvements or additions to documentation triaged/resolved working as intended

Comments

@koumdev
Copy link

koumdev commented Apr 11, 2024

Hi,

I'm trying to use ExecuteStateTransactionAsync() with Azure Cosmos DB state store, but it does not work. I keep getting "error while executing state transaction: transaction failed due to operation 0 which failed with status code 400" exception. Using .NET or the API directly does not make a difference. I am using Dapr version 1.13.0.

I tried what is in the Dapr state management documentation here:

Expected Behavior

Executing transaction operations should work.

Actual Behavior

An exception is thrown : "error while executing state transaction: transaction failed due to operation 0 which failed with status code 400"

Steps to Reproduce the Problem

This should work:

var random = new Random();
int orderId = random.Next(1, 1000);
var requests = new List<StateTransactionRequest>()
{
    new("order_1", JsonSerializer.SerializeToUtf8Bytes(orderId.ToString()), StateOperationType.Upsert)
};
await _daprClient.ExecuteStateTransactionAsync(STORE_NAME, requests, cancellationToken: cancellation);

Using PowerShell to target the API directly should also work:

Invoke-RestMethod -Method Post -ContentType 'application/json' -Body '{"operations": [{"operation":"upsert", "request": {"key": "order_1", "value": "150"}}, {"operation":"upsert", "request": {"key": "order_2", "value": "250"}}]}' -Uri 'http://localhost:3500/v1.0/state/statestorecosmos/transaction'

@berndverst
Copy link
Member

Take a look at the example here:
https://docs.dapr.io/reference/api/state_api/#examples

For CosmosDB you must specify the metadata property partitionKey which will apply to all operations within your transaction. CosmosDB does not support transaction across partitions.

If you include the metadata property (which has to be done manually) this will work.

@berndverst berndverst added documentation Improvements or additions to documentation working as intended triaged/resolved and removed kind/bug Something isn't working labels Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation triaged/resolved working as intended
Projects
None yet
Development

No branches or pull requests

2 participants