Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit a9375d9

Browse files
committed
Add MultiThreadedUpdateTransactionIssue
1 parent d2ccc18 commit a9375d9

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System.Threading;
2+
using NUnit.Framework;
3+
using ServiceStack.DataAnnotations;
4+
5+
namespace ServiceStack.OrmLite.Tests.Issues
6+
{
7+
[Explicit]
8+
public class MultiThreadedUpdateTransactionIssue : OrmLiteTestBase
9+
{
10+
public class ModelWithIdAndName
11+
{
12+
[AutoIncrement]
13+
public int Id { get; set; }
14+
public string Name { get; set; }
15+
}
16+
17+
[Test]
18+
public void Can_Insert_Update_record_across_multiple_threads()
19+
{
20+
using (var db = OpenDbConnection())
21+
{
22+
db.DropAndCreateTable<ModelWithIdAndName>();
23+
}
24+
25+
int count = 0;
26+
27+
20.Times(i =>
28+
{
29+
ThreadPool.QueueUserWorkItem(state =>
30+
{
31+
40.Times(_ =>
32+
{
33+
using (var db = OpenDbConnection())
34+
{
35+
var objA = new ModelWithIdAndName { Name = "A" };
36+
var objB = new ModelWithIdAndName { Name = "B" };
37+
38+
objA.Id = (int)db.Insert(objA, selectIdentity: true);
39+
40+
objB.Id = (int)db.Insert(objB, selectIdentity: true);
41+
objB.Name = objA.Name;
42+
43+
db.Update(objB);
44+
Interlocked.Increment(ref count);
45+
}
46+
});
47+
});
48+
});
49+
50+
Thread.Sleep(5000);
51+
Assert.That(count, Is.EqualTo(20 * 40));
52+
}
53+
}
54+
}

tests/ServiceStack.OrmLite.Tests/ServiceStack.OrmLite.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@
144144
<Compile Include="Issues\JsonUpdateOnlyIssue.cs" />
145145
<Compile Include="Issues\LoadReferencesNullReferenceIssue.cs" />
146146
<Compile Include="Issues\MergeParamsIssue.cs" />
147+
<Compile Include="Issues\MultiThreadedUpdateTransactionIssue.cs" />
147148
<Compile Include="Issues\ParamNameIssues.cs" />
148149
<Compile Include="MultipleConnectionIdTests.cs" />
149150
<Compile Include="UseCase\CustomerOrdersUseCaseAsync.cs" />

0 commit comments

Comments
 (0)