diff --git a/src/SQLite.cs b/src/SQLite.cs index 0ce075e0..c1eac952 100644 --- a/src/SQLite.cs +++ b/src/SQLite.cs @@ -1710,21 +1710,20 @@ PreparedSqlLiteInsertCommand GetInsertCommand (TableMapping map, string extra) var key = Tuple.Create (map.MappedType.FullName, extra); lock (_insertCommandMap) { - _insertCommandMap.TryGetValue (key, out prepCmd); + if (_insertCommandMap.TryGetValue (key, out prepCmd)) { + return prepCmd; + } } - if (prepCmd == null) { - prepCmd = CreateInsertCommand (map, extra); - var added = false; - lock (_insertCommandMap) { - if (!_insertCommandMap.ContainsKey (key)) { - _insertCommandMap.Add (key, prepCmd); - added = true; - } - } - if (!added) { + prepCmd = CreateInsertCommand (map, extra); + + lock (_insertCommandMap) { + if (_insertCommandMap.TryGetValue (key, out var existing)) { prepCmd.Dispose (); + return existing; } + + _insertCommandMap.Add (key, prepCmd); } return prepCmd; diff --git a/tests/ConcurrencyTest.cs b/tests/ConcurrencyTest.cs index 358a1133..ebf865bd 100644 --- a/tests/ConcurrencyTest.cs +++ b/tests/ConcurrencyTest.cs @@ -187,7 +187,28 @@ public void TestLoad() } } - + /// + /// Test for issue #761. Because the nature of this test is a race condition, + /// it is not guaranteed to fail if the issue is present. It does appear to + /// fail most of the time, though. + /// + [Test] + public void TestInsertCommandCreation () + { + using (var dbConnection = + new DbConnection (SQLiteOpenFlags.FullMutex | SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create)) { + var obj1 = new TestObj (); + var obj2 = new TestObj (); + var taskA = Task.Run (() => { + dbConnection.Insert (obj1); + }); + var taskB = Task.Run (() => { + dbConnection.Insert (obj2); + }); + + Task.WhenAll (taskA, taskB).Wait (); + } + } } }