Skip to content

Commit 31f24c3

Browse files
tofutimoysteinkrog
authored andcommitted
Add _locker to PreparedSqlLiteInsertCommand to pass StressAsync
1 parent 715ac57 commit 31f24c3

2 files changed

Lines changed: 19 additions & 4 deletions

File tree

src/SQLite.Net/PreparedSqlLiteInsertCommand.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
using System;
2525
using JetBrains.Annotations;
2626
using SQLite.Net.Interop;
27+
using System.Diagnostics;
2728

2829
namespace SQLite.Net
2930
{
@@ -63,11 +64,12 @@ public void Dispose()
6364
Dispose(false);
6465
}
6566

67+
static readonly object _locker = new object();
68+
6669
[PublicAPI]
6770
public int ExecuteNonQuery(object[] source)
6871
{
6972
Connection.TraceListener.WriteLine("Executing: {0}", CommandText);
70-
7173
if (!Initialized)
7274
{
7375
Statement = Prepare();
@@ -84,7 +86,12 @@ public int ExecuteNonQuery(object[] source)
8486
Connection.StoreDateTimeAsTicks, Connection.Serializer);
8587
}
8688
}
87-
var r = sqlitePlatform.SQLiteApi.Step(Statement);
89+
90+
Result r;
91+
lock (_locker)
92+
{
93+
r = sqlitePlatform.SQLiteApi.Step(Statement);
94+
}
8895

8996
if (r == Result.Done)
9097
{
@@ -104,6 +111,7 @@ public int ExecuteNonQuery(object[] source)
104111
throw NotNullConstraintViolationException.New(r, sqlitePlatform.SQLiteApi.Errmsg16(Connection.Handle));
105112
}
106113
sqlitePlatform.SQLiteApi.Reset(Statement);
114+
107115
throw SQLiteException.New(r, r.ToString());
108116
}
109117

@@ -130,4 +138,4 @@ private void Dispose(bool disposing)
130138
}
131139
}
132140
}
133-
}
141+
}

tests/AsyncTests.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,11 @@ public async Task StressAsync()
190190
{
191191
tasks.Add(Task.Factory.StartNew(async delegate
192192
{
193+
string taskDesc = "";
194+
193195
try
194196
{
197+
taskDesc = "CONNECT";
195198
SQLiteAsyncConnection conn = GetAsyncConnection();
196199

197200
// each connection retains the global journal_mode but somehow resets busy_timeout to 100
@@ -203,6 +206,8 @@ public async Task StressAsync()
203206
{
204207
FirstName = i.ToString(),
205208
};
209+
210+
taskDesc = "INSERT";
206211
await conn.InsertAsync(obj);
207212

208213
if (obj.Id == 0)
@@ -212,6 +217,8 @@ public async Task StressAsync()
212217
errors.Add("Bad Id");
213218
}
214219
}
220+
221+
taskDesc = "SELECT";
215222
var obj3 = await (from c in conn.Table<Customer>() where c.Id == obj.Id select c).ToListAsync();
216223
Customer obj2 = obj3.FirstOrDefault();
217224
if (obj2 == null)
@@ -226,7 +233,7 @@ public async Task StressAsync()
226233
{
227234
lock (errors)
228235
{
229-
errors.Add(ex.Message);
236+
errors.Add(string.Format("{0}: {1}", taskDesc, ex.Message));
230237
}
231238
}
232239
}));

0 commit comments

Comments
 (0)