forked from googleapis/dotnet-spanner-entity-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransactionSample.cs
80 lines (72 loc) · 3.66 KB
/
TransactionSample.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright 2021 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using Google.Cloud.EntityFrameworkCore.Spanner.Samples.SampleModel;
using Microsoft.EntityFrameworkCore;
using System;
using System.Threading.Tasks;
/// <summary>
/// By default all changes in a single call to SaveChanges are applied in a transaction.
///
/// You can also manually control transactions if you want to group multiple SaveChanges
/// in a single transaction.
///
/// See https://docs.microsoft.com/en-us/ef/core/saving/transactions for more information
/// on how to control transactions with Entity Framework Core.
///
/// NOTE: Cloud Spanner does not support Savepoints.
///
/// Run from the command line with `dotnet run TransactionSample`
/// </summary>
public static class TransactionSample
{
public static async Task Run(string connectionString)
{
using var context = new SpannerSampleDbContext(connectionString);
// Start a read/write transaction that will be used with the database context.
using var transaction = await context.Database.BeginTransactionAsync();
// Create a new Singer, add it to the context and save the changes.
// These changes have not yet been committed to the database and are
// therefore not readable for other processes.
var singerId = Guid.NewGuid();
await context.Singers.AddAsync(new Singer
{
SingerId = singerId,
FirstName = "Bernhard",
LastName = "Bennet"
});
var count = await context.SaveChangesAsync();
Console.WriteLine($"Added {count} singer in a transaction.");
// Now try to read the singer in a different context which will use a different transaction.
// This will return null, as pending changes from other transactions are not visible.
using var contextWithoutTransaction = new SpannerSampleDbContext(connectionString);
var exists = await contextWithoutTransaction.Singers
.FromSqlInterpolated($"SELECT * FROM Singers WHERE SingerId={singerId}")
.FirstOrDefaultAsync();
Console.WriteLine($"Can read singer outside of transaction: {exists != null}");
// Now try to read the same using the context with the transaction. This will return true as
// a transaction can read its own writes. The Cloud Spanner Entity Framework Core provider
// uses DML by default for updates that are executed in manual transactions in order to support
// the read-your-writes feature.
exists = await context.Singers
.FromSqlInterpolated($"SELECT * FROM Singers WHERE SingerId={singerId}")
.FirstOrDefaultAsync();
Console.WriteLine($"Can read singer inside transaction: {exists != null}");
// Commit the transaction. The singer is now also readable in a context without the transaction.
await transaction.CommitAsync();
exists = await contextWithoutTransaction.Singers
.FromSqlInterpolated($"SELECT * FROM Singers WHERE SingerId={singerId}")
.FirstOrDefaultAsync();
Console.WriteLine($"Can read singer after commit: {exists != null}");
}
}