forked from dotnet/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
62 lines (54 loc) · 1.98 KB
/
Program.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
using System;
using System.Data;
using Microsoft.Data.Sqlite;
namespace DirtyReadSample
{
class Program
{
static void Main()
{
// The connections must use a shared cache
const string connectionString = "Data Source=DirtyReadSample;Mode=Memory;Cache=Shared";
var firstConnection = new SqliteConnection(connectionString);
firstConnection.Open();
var secondConnection = new SqliteConnection(connectionString);
secondConnection.Open();
var createCommand = firstConnection.CreateCommand();
createCommand.CommandText =
@"
CREATE TABLE data (
value TEXT
);
INSERT INTO data
VALUES ('clean');
";
createCommand.ExecuteNonQuery();
#region snippet_DirtyRead
using (var firstTransaction = firstConnection.BeginTransaction())
{
var updateCommand = firstConnection.CreateCommand();
updateCommand.CommandText =
@"
UPDATE data
SET value = 'dirty'
";
updateCommand.ExecuteNonQuery();
// Without ReadUncommitted, the command will time out since the table is locked
// while the transaction on the first connection is active
using (secondConnection.BeginTransaction(IsolationLevel.ReadUncommitted))
{
var queryCommand = secondConnection.CreateCommand();
queryCommand.CommandText =
@"
SELECT *
FROM data
";
var value = (string)queryCommand.ExecuteScalar();
Console.WriteLine($"Value: {value}");
}
firstTransaction.Rollback();
}
#endregion
}
}
}