Skip to content

Commit fabc280

Browse files
Handle CacheBase.Destroy in the session factory
1 parent 223e90d commit fabc280

10 files changed

+38
-51
lines changed

src/NHibernate/Async/Impl/SessionFactoryImpl.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,11 @@ public sealed partial class SessionFactoryImpl : ISessionFactoryImplementor, IOb
8989
{
9090
cache.Value.Destroy();
9191
}
92+
}
9293

93-
updateTimestampsCache.Destroy();
94+
foreach (var cache in _allCacheRegions.Values)
95+
{
96+
cache.Destroy();
9497
}
9598

9699
settings.CacheProvider.Stop();

src/NHibernate/Cache/CacheFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static ICacheConcurrencyStrategy CreateCache(
4343

4444
var cache = BuildCacheBase(name, settings, properties);
4545

46-
var ccs = CreateCache(usage,cache);
46+
var ccs = CreateCache(usage, cache);
4747

4848
if (mutable && usage == ReadOnly)
4949
log.Warn("read-only cache configured for mutable: {0}", name);

src/NHibernate/Cache/ICacheConcurrencyStrategy.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,12 @@ public partial interface ICacheConcurrencyStrategy
124124
void Clear();
125125

126126
/// <summary>
127-
/// Clean up all resources.
127+
/// Clean up resources.
128128
/// </summary>
129129
/// <exception cref="CacheException"></exception>
130+
/// <remarks>
131+
/// This method should not destroy <see cref="Cache" />. The session factory is responsible for it.
132+
/// </remarks>
130133
void Destroy();
131134

132135
/// <summary>

src/NHibernate/Cache/IQueryCache.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,11 @@ public partial interface IQueryCache
4444
IList Get(QueryKey key, ICacheAssembler[] returnTypes, bool isNaturalKeyLookup, ISet<string> spaces, ISessionImplementor session);
4545

4646
/// <summary>
47-
/// Clean up all resources.
47+
/// Clean up resources.
4848
/// </summary>
49+
/// <remarks>
50+
/// This method should not destroy <see cref="Cache" />. The session factory is responsible for it.
51+
/// </remarks>
4952
void Destroy();
5053
}
5154

src/NHibernate/Cache/NonstrictReadWriteCache.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -203,14 +203,9 @@ public void Clear()
203203

204204
public void Destroy()
205205
{
206-
try
207-
{
208-
Cache.Destroy();
209-
}
210-
catch (Exception e)
211-
{
212-
log.Warn(e, "Could not destroy cache");
213-
}
206+
// The cache is externally provided and may be shared. Destroying the cache is
207+
// not the responsibility of this class.
208+
Cache = null;
214209
}
215210

216211
/// <summary>

src/NHibernate/Cache/ReadOnlyCache.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,9 @@ public void Remove(CacheKey key)
181181

182182
public void Destroy()
183183
{
184-
try
185-
{
186-
Cache.Destroy();
187-
}
188-
catch (Exception e)
189-
{
190-
log.Warn(e, "Could not destroy cache");
191-
}
184+
// The cache is externally provided and may be shared. Destroying the cache is
185+
// not the responsibility of this class.
186+
Cache = null;
192187
}
193188

194189
/// <summary>

src/NHibernate/Cache/ReadWriteCache.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -413,14 +413,9 @@ public void Remove(CacheKey key)
413413

414414
public void Destroy()
415415
{
416-
try
417-
{
418-
Cache.Destroy();
419-
}
420-
catch (Exception e)
421-
{
422-
log.Warn(e, "Could not destroy cache");
423-
}
416+
// The cache is externally provided and may be shared. Destroying the cache is
417+
// not the responsibility of this class.
418+
Cache = null;
424419
}
425420

426421
/// <summary>

src/NHibernate/Cache/StandardQueryCache.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -281,14 +281,8 @@ public IList[] GetMany(
281281

282282
public void Destroy()
283283
{
284-
try
285-
{
286-
Cache.Destroy();
287-
}
288-
catch (Exception e)
289-
{
290-
Log.Warn(e, "could not destroy query cache: {0}", _regionName);
291-
}
284+
// The cache is externally provided and may be shared. Destroying the cache is
285+
// not the responsibility of this class.
292286
}
293287

294288
#endregion

src/NHibernate/Cache/UpdateTimestampsCache.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,16 +155,12 @@ public virtual bool[] AreUpToDate(ISet<string>[] spaces, long[] timestamps)
155155
return results;
156156
}
157157

158+
// Since v5.2
159+
[Obsolete("This method has no usages anymore")]
158160
public virtual void Destroy()
159161
{
160-
try
161-
{
162-
_updateTimestamps.Destroy();
163-
}
164-
catch (Exception e)
165-
{
166-
log.Warn(e, "could not destroy UpdateTimestamps cache");
167-
}
162+
// The cache is externally provided and may be shared. Destroying the cache is
163+
// not the responsibility of this class.
168164
}
169165

170166
private bool IsOutdated(long? lastUpdate, long timestamp)

src/NHibernate/Impl/SessionFactoryImpl.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ public SessionFactoryImpl(Configuration cfg, IMapping mapping, Settings settings
368368
if (settings.IsQueryCacheEnabled)
369369
{
370370
var updateTimestampsCacheName = typeof(UpdateTimestampsCache).Name;
371-
updateTimestampsCache = new UpdateTimestampsCache(BuildCache(updateTimestampsCacheName));
371+
updateTimestampsCache = new UpdateTimestampsCache(GetCache(updateTimestampsCacheName));
372372
var queryCacheName = typeof(StandardQueryCache).FullName;
373373
queryCache = BuildQueryCache(queryCacheName);
374374
queryCaches = new ConcurrentDictionary<string, Lazy<IQueryCache>>();
@@ -415,7 +415,7 @@ private IQueryCache BuildQueryCache(string queryCacheName)
415415
settings.QueryCacheFactory.GetQueryCache(
416416
updateTimestampsCache,
417417
properties,
418-
BuildCache(queryCacheName))
418+
GetCache(queryCacheName))
419419
// 6.0 TODO: remove the coalesce once IQueryCacheFactory todos are done
420420
#pragma warning disable 618
421421
?? settings.QueryCacheFactory.GetQueryCache(
@@ -432,14 +432,14 @@ private ICacheConcurrencyStrategy GetCacheConcurrencyStrategy(
432432
bool isMutable,
433433
Dictionary<Tuple<string, string>, ICacheConcurrencyStrategy> caches)
434434
{
435-
var cacheKey = new Tuple<string, string>(cacheRegion, strategy);
436435
if (strategy == null || !settings.IsSecondLevelCacheEnabled)
437436
return null;
438437

438+
var cacheKey = new Tuple<string, string>(cacheRegion, strategy);
439439
if (caches.TryGetValue(cacheKey, out var cache))
440440
return cache;
441-
442-
cache = CacheFactory.CreateCache(strategy, BuildCache(cacheRegion));
441+
442+
cache = CacheFactory.CreateCache(strategy, GetCache(cacheRegion));
443443
caches.Add(cacheKey, cache);
444444
if (isMutable && strategy == CacheFactory.ReadOnly)
445445
log.Warn("read-only cache configured for mutable: {0}", name);
@@ -878,8 +878,11 @@ public void Close()
878878
{
879879
cache.Value.Destroy();
880880
}
881+
}
881882

882-
updateTimestampsCache.Destroy();
883+
foreach (var cache in _allCacheRegions.Values)
884+
{
885+
cache.Destroy();
883886
}
884887

885888
settings.CacheProvider.Stop();
@@ -1083,7 +1086,7 @@ public ICache GetSecondLevelCacheRegion(string regionName)
10831086
return result;
10841087
}
10851088

1086-
private CacheBase BuildCache(string cacheRegion)
1089+
private CacheBase GetCache(string cacheRegion)
10871090
{
10881091
// If run concurrently for the same region and type, this may built many caches for the same region and type.
10891092
// Currently only GetQueryCache may be run concurrently, and its implementation prevents

0 commit comments

Comments
 (0)