Skip to content

Commit 6e8fd09

Browse files
committed
Merge pull request #223 from molinch/my-master
Fix #222
2 parents 61557e0 + 64f673a commit 6e8fd09

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

src/SQLite.Net/SQLiteConnection.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public class SQLiteConnection : IDisposable
5252
#pragma warning restore 649
5353
private readonly Random _rand = new Random();
5454
private readonly IDictionary<string, TableMapping> _tableMappings;
55+
private readonly object _tableMappingsLocks;
5556
private TimeSpan _busyTimeout;
5657
private long _elapsedMilliseconds;
5758
private IDictionary<TableMapping, ActiveInsertCommand> _insertCommandCache;
@@ -152,6 +153,7 @@ public SQLiteConnection([JetBrains.Annotations.NotNull] ISQLitePlatform sqlitePl
152153
Resolver = resolver ?? ContractResolver.Current;
153154

154155
_tableMappings = tableMappings ?? new Dictionary<string, TableMapping>();
156+
_tableMappingsLocks = new object();
155157

156158
if (string.IsNullOrEmpty(databasePath))
157159
{
@@ -235,7 +237,13 @@ public TimeSpan BusyTimeout
235237
[JetBrains.Annotations.NotNull]
236238
public IEnumerable<TableMapping> TableMappings
237239
{
238-
get { return _tableMappings.Values; }
240+
get
241+
{
242+
lock (_tableMappingsLocks)
243+
{
244+
return _tableMappings.Values.ToList();
245+
}
246+
}
239247
}
240248

241249
/// <summary>
@@ -293,16 +301,19 @@ private static byte[] GetNullTerminatedUtf8(string s)
293301
[PublicAPI]
294302
public TableMapping GetMapping(Type type, CreateFlags createFlags = CreateFlags.None)
295303
{
296-
TableMapping map;
297-
return _tableMappings.TryGetValue(type.FullName, out map) ? map : CreateAndSetMapping(type, createFlags, _tableMappings);
304+
lock (_tableMappingsLocks)
305+
{
306+
TableMapping map;
307+
return _tableMappings.TryGetValue(type.FullName, out map) ? map : CreateAndSetMapping(type, createFlags, _tableMappings);
308+
}
298309
}
299310

300311
private TableMapping CreateAndSetMapping(Type type, CreateFlags createFlags, IDictionary<string, TableMapping> mapTable)
301312
{
302313
var props = Platform.ReflectionService.GetPublicInstanceProperties(type);
303314
var map = new TableMapping(type, props, createFlags);
304-
mapTable[type.FullName] = map;
305-
return map;
315+
mapTable[type.FullName] = map;
316+
return map;
306317
}
307318

308319
/// <summary>

0 commit comments

Comments
 (0)