Light
Dark
System
v4latest
v5dev
v4latest
v3
v2
v1

Transactions

Transactions allow you to execute transactional code via the TransactionAsync API, retrying your queries if a retryable error (e.g. a network failure) occurs. If an non-retryable error happens, the queries performed within the transactions are automatically rolled back:

C#
F#
Copy
var client = new EdgeDBClient();

await client.TransactionAsync(async tx =>
    await tx.ExecuteAsync("INSERT User { name := 'John Smith' }");
);
Copy
let client = new EdgeDBClient()

client.TransactionAsync(
  fun tx -> tx.ExecuteAsync("INSERT User { name := 'John Smith' }")
) |> Async.AwaitTask |> Async.Ignore |> ignore

Code blocks in transactions may run multiple times. It’s good practice to only perform safe to re-run operations in transaction blocks.

The TransactionAsync method proxies the result of the transaction, allowing you to get the result of a query executed in a transaction:

C#
F#
Copy
var client = new EdgeDBClient();

var transactionResult = await client.TransactionAsync(async tx =>
    await tx.QueryRequiredSingleAsync<string>("SELECT 'Hello from Transaction!'");
);

Console.WriteLine(transactionResult);
Copy
let client = new EdgeDBClient()

client.TransactionAsync(
  fun tx -> tx.QueryRequiredSingleAsync<string>("SELECT 'Hello from Transaction!'")
)
|> Async.AwaitTask
|> Async.RunSynchronously
|> fun r -> printfn "Transaction result: %s" r
Light
Dark
System

We use ChatGPT with additional context from our documentation to answer your questions. Not all answers will be accurate. Please join our Discord if you need more help.