-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
.. _edgedb-dotnet-transactions: | ||
|
||
============ | ||
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: | ||
|
||
.. tabs:: | ||
|
||
.. code-tab:: cs | ||
:caption: C# | ||
|
||
var client = new EdgeDBClient(); | ||
|
||
await client.TransactionAsync(async tx => | ||
await tx.ExecuteAsync("INSERT User { name := 'John Smith' }"); | ||
); | ||
|
||
.. code-tab:: fsharp | ||
:caption: F# | ||
|
||
let client = new EdgeDBClient() | ||
|
||
client.TransactionAsync( | ||
fun tx -> tx.ExecuteAsync("INSERT User { name := 'John Smith' }") | ||
) |> Async.AwaitTask |> Async.Ignore |> ignore | ||
.. note:: | ||
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: | ||
|
||
.. tabs:: | ||
|
||
.. code-tab:: cs | ||
:caption: C# | ||
|
||
var client = new EdgeDBClient(); | ||
|
||
var transactionResult = await client.TransactionAsync(async tx => | ||
await tx.QueryRequiredSingleAsync<string>("SELECT 'Hello from Transaction!'"); | ||
); | ||
|
||
Console.WriteLine(transactionResult); | ||
|
||
.. code-tab:: fsharp | ||
:caption: F# | ||
|
||
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 | ||