jaquel / dapper-dot-net

Automatically exported from code.google.com/p/dapper-dot-net
Other
0 stars 0 forks source link

SqlMapper.typeMap dictionary is not thread-safe #165

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
- SqlMapper exposes a static method `AddTypeMap` to write to the typeMap 
dictionary.

- Since this method can be called from multiple threads, it needs to be 
thread-safe.

- `typeMap` currently uses a standard Dictionary<K,V> that isn't thread-safe.

The fix would be to use ConcurrentDictionary<K,V> (.NET 4+) or to implement 
AddTypeMap in a thread-safe way (e.g. clone the dictionary, update then 
replace).

Original issue reported on code.google.com by Jocular...@hotmail.com on 25 Nov 2013 at 2:34

GoogleCodeExporter commented 9 years ago
Hmmm, indeed. Originally this was essentially internal only, but the addition 
of AddTypeMap requires synchronisation. I would, however, be keep to avoid any 
unnecessary overheads here. It is a shame that the value here is value-typed, 
otherwise Hashtable could be used very effectively (it has very good threading 
semantics). I suspect the most appropriate "fix" here is for `AddTypeMap` to 
*clone, mutate and replace* the dictionary - avoiding any need for 
thread-safety for readers (a dereference is always safe).

Original comment by marc.gravell on 25 Nov 2013 at 2:45

GoogleCodeExporter commented 9 years ago
I agree that clone, mutate and replace is probably the way to go.

And perhaps add a public overload AddTypeMap(IEnumerable<KeyValuePair<Type, 
DbType>) so that a caller can more efficiently add multiple entries to the 
dictionary.

Original comment by Jocular...@hotmail.com on 25 Nov 2013 at 4:35

GoogleCodeExporter commented 9 years ago
Implemented for both type-map and type-handlers

Original comment by marc.gravell on 5 Aug 2014 at 3:43