11using System ;
2+ using System . Collections . Concurrent ;
23using System . Collections . Generic ;
34using System . Data ;
45using System . Linq ;
@@ -93,8 +94,8 @@ public void HandleEntityNotFound(string entityName, object id)
9394 private static readonly IIdentifierGenerator UuidGenerator = new UUIDHexGenerator ( ) ;
9495
9596 [ NonSerialized ]
96- private readonly ThreadSafeDictionary < string , ICache > allCacheRegions =
97- new ThreadSafeDictionary < string , ICache > ( new Dictionary < string , ICache > ( ) ) ;
97+ private readonly ConcurrentDictionary < string , ICache > allCacheRegions =
98+ new ConcurrentDictionary < string , ICache > ( ) ;
9899
99100 [ NonSerialized ]
100101 private readonly IDictionary < string , IClassMetadata > classMetadata ;
@@ -147,7 +148,7 @@ public void HandleEntityNotFound(string entityName, object id)
147148 private readonly IQueryCache queryCache ;
148149
149150 [ NonSerialized ]
150- private readonly IDictionary < string , IQueryCache > queryCaches ;
151+ private readonly ConcurrentDictionary < string , IQueryCache > queryCaches ;
151152 [ NonSerialized ]
152153 private readonly SchemaExport schemaExport ;
153154 [ NonSerialized ]
@@ -160,7 +161,7 @@ public void HandleEntityNotFound(string entityName, object id)
160161 [ NonSerialized ]
161162 private readonly UpdateTimestampsCache updateTimestampsCache ;
162163 [ NonSerialized ]
163- private readonly IDictionary < string , string [ ] > entityNameImplementorsMap = new ThreadSafeDictionary < string , string [ ] > ( new Dictionary < string , string [ ] > ( 100 ) ) ;
164+ private readonly IDictionary < string , string [ ] > entityNameImplementorsMap = new ConcurrentDictionary < string , string [ ] > ( 4 * System . Environment . ProcessorCount , 100 ) ;
164165 private readonly string uuid ;
165166 private bool disposed ;
166167
@@ -247,7 +248,10 @@ public SessionFactoryImpl(Configuration cfg, IMapping mapping, Settings settings
247248 if ( cache != null )
248249 {
249250 caches . Add ( cacheRegion , cache ) ;
250- allCacheRegions . Add ( cache . RegionName , cache . Cache ) ;
251+ if ( ! allCacheRegions . TryAdd ( cache . RegionName , cache . Cache ) )
252+ {
253+ throw new HibernateException ( "An item with the same key has already been added to allCacheRegions." ) ;
254+ }
251255 }
252256 }
253257 IEntityPersister cp = PersisterFactory . CreateClassPersister ( model , cache , this , mapping ) ;
@@ -375,7 +379,7 @@ public SessionFactoryImpl(Configuration cfg, IMapping mapping, Settings settings
375379 {
376380 updateTimestampsCache = new UpdateTimestampsCache ( settings , properties ) ;
377381 queryCache = settings . QueryCacheFactory . GetQueryCache ( null , updateTimestampsCache , settings , properties ) ;
378- queryCaches = new ThreadSafeDictionary < string , IQueryCache > ( new Dictionary < string , IQueryCache > ( ) ) ;
382+ queryCaches = new ConcurrentDictionary < string , IQueryCache > ( ) ;
379383 }
380384 else
381385 {
@@ -967,10 +971,8 @@ public UpdateTimestampsCache UpdateTimestampsCache
967971
968972 public IDictionary < string , ICache > GetAllSecondLevelCacheRegions ( )
969973 {
970- lock ( allCacheRegions . SyncRoot )
971- {
972- return new Dictionary < string , ICache > ( allCacheRegions ) ;
973- }
974+ // ToArray creates a moment in time snapshot
975+ return allCacheRegions . ToArray ( ) . ToDictionary ( kv => kv . Key , kv => kv . Value ) ;
974976 }
975977
976978 public ICache GetSecondLevelCacheRegion ( string regionName )
@@ -1002,18 +1004,14 @@ public IQueryCache GetQueryCache(string cacheRegion)
10021004 return null ;
10031005 }
10041006
1005- lock ( allCacheRegions . SyncRoot )
1006- {
1007- IQueryCache currentQueryCache ;
1008- if ( ! queryCaches . TryGetValue ( cacheRegion , out currentQueryCache ) )
1007+ return queryCaches . GetOrAdd (
1008+ cacheRegion ,
1009+ cr =>
10091010 {
1010- currentQueryCache =
1011- settings . QueryCacheFactory . GetQueryCache ( cacheRegion , updateTimestampsCache , settings , properties ) ;
1012- queryCaches [ cacheRegion ] = currentQueryCache ;
1011+ IQueryCache currentQueryCache = settings . QueryCacheFactory . GetQueryCache ( cacheRegion , updateTimestampsCache , settings , properties ) ;
10131012 allCacheRegions [ currentQueryCache . RegionName ] = currentQueryCache . Cache ;
1014- }
1015- return currentQueryCache ;
1016- }
1013+ return currentQueryCache ;
1014+ } ) ;
10171015 }
10181016
10191017 public void EvictQueries ( )
@@ -1277,4 +1275,4 @@ public string Uuid
12771275
12781276 #endregion
12791277 }
1280- }
1278+ }
0 commit comments