Skip to content

Guards against use of a disposed session factory #3120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/NHibernate.Test/Async/SessionBuilder/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,5 +227,22 @@ private void CanSetOnStateless<T, V>(
Assert.AreEqual(sb, fsb, $"{sbType}: Unexpected fluent return after call with {value}");
}
}

[Test]
public void ThrowWhenUsingSessionFromDisposedFactoryAsync()
{
using (var session = Sfi.OpenSession())
{
try
{
Sfi.Dispose();
Assert.That(() => session.GetAsync<Entity>(Guid.Empty), Throws.InstanceOf(typeof(ObjectDisposedException)));
}
finally
{
RebuildSessionFactory();
}
}
}
}
}
31 changes: 31 additions & 0 deletions src/NHibernate.Test/SessionBuilder/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,5 +298,36 @@ private void CanSetOnStateless<T, V>(
Assert.AreEqual(sb, fsb, $"{sbType}: Unexpected fluent return after call with {value}");
}
}

[Test]
public void ThrowWhenOpeningFromDisposedFactory()
{
try
{
Sfi.Dispose();
Assert.That(Sfi.OpenSession, Throws.InstanceOf(typeof(ObjectDisposedException)));
}
finally
{
RebuildSessionFactory();
}
}

[Test]
public void ThrowWhenUsingSessionFromDisposedFactory()
{
using (var session = Sfi.OpenSession())
{
try
{
Sfi.Dispose();
Assert.That(() => session.Get<Entity>(Guid.Empty), Throws.InstanceOf(typeof(ObjectDisposedException)));
}
finally
{
RebuildSessionFactory();
}
}
}
}
}
2 changes: 2 additions & 0 deletions src/NHibernate/Async/Impl/SessionFactoryImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ async Task InternalEvictCollectionAsync()
// NH Different implementation
if (queryCache != null)
{
CheckNotClosed();
await (queryCache.ClearAsync(cancellationToken)).ConfigureAwait(false);
if (queryCaches.Count == 0)
{
Expand All @@ -377,6 +378,7 @@ async Task InternalEvictCollectionAsync()
{
if (settings.IsQueryCacheEnabled)
{
CheckNotClosed();
if (queryCaches.TryGetValue(cacheRegion, out var currentQueryCache))
{
return currentQueryCache.Value.ClearAsync(cancellationToken);
Expand Down
31 changes: 29 additions & 2 deletions src/NHibernate/Impl/SessionFactoryImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,11 @@ private ICacheConcurrencyStrategy GetCacheConcurrencyStrategy(

public EventListeners EventListeners
{
get { return eventListeners; }
get
{
CheckNotClosed();
return eventListeners;
}
}

#region IObjectReference Members
Expand Down Expand Up @@ -513,6 +517,7 @@ public object GetRealObject(StreamingContext context)

public ISessionBuilder WithOptions()
{
CheckNotClosed();
return new SessionBuilderImpl(this);
}

Expand Down Expand Up @@ -563,6 +568,7 @@ public ISession OpenSession(DbConnection connection, bool flushBeforeCompletionE

public IStatelessSessionBuilder WithStatelessOptions()
{
CheckNotClosed();
return new StatelessSessionBuilderImpl(this);
}

Expand All @@ -580,6 +586,7 @@ public IStatelessSession OpenStatelessSession(DbConnection connection)

public IEntityPersister GetEntityPersister(string entityName)
{
CheckNotClosed();
IEntityPersister value;
if (entityPersisters.TryGetValue(entityName, out value) == false)
throw new MappingException("No persister for: " + entityName);
Expand All @@ -588,13 +595,15 @@ public IEntityPersister GetEntityPersister(string entityName)

public IEntityPersister TryGetEntityPersister(string entityName)
{
CheckNotClosed();
IEntityPersister result;
entityPersisters.TryGetValue(entityName, out result);
return result;
}

public ICollectionPersister GetCollectionPersister(string role)
{
CheckNotClosed();
ICollectionPersister value;
if (collectionPersisters.TryGetValue(role, out value) == false)
throw new MappingException("Unknown collection role: " + role);
Expand Down Expand Up @@ -863,6 +872,14 @@ public void Dispose()
Close();
}

private void CheckNotClosed()
{
if (isClosed)
{
throw new ObjectDisposedException($"Session factory {Name} with id {Uuid}");
}
}

/// <summary>
/// Closes the session factory, releasing all held resources.
/// <list>
Expand Down Expand Up @@ -1105,6 +1122,7 @@ public UpdateTimestampsCache UpdateTimestampsCache
public IDictionary<string, ICache> GetAllSecondLevelCacheRegions()
#pragma warning restore 618
{
CheckNotClosed();
return
_allCacheRegions
// ToArray creates a moment in time snapshot
Expand All @@ -1119,6 +1137,7 @@ public IDictionary<string, ICache> GetAllSecondLevelCacheRegions()
public ICache GetSecondLevelCacheRegion(string regionName)
#pragma warning restore 618
{
CheckNotClosed();
_allCacheRegions.TryGetValue(regionName, out var result);
return result;
}
Expand All @@ -1145,7 +1164,12 @@ public IStatisticsImplementor StatisticsImplementor

public IQueryCache QueryCache
{
get { return queryCache; }
get
{
if (queryCache != null)
CheckNotClosed();
return queryCache;
}
}

public IQueryCache GetQueryCache(string cacheRegion)
Expand All @@ -1159,6 +1183,7 @@ public IQueryCache GetQueryCache(string cacheRegion)
return null;
}

CheckNotClosed();
// The factory may be run concurrently by threads trying to get the same region.
// But the GetOrAdd will yield the same lazy for all threads, so only one will
// initialize. https://stackoverflow.com/a/31637510/1178314
Expand All @@ -1175,6 +1200,7 @@ public void EvictQueries()
// NH Different implementation
if (queryCache != null)
{
CheckNotClosed();
queryCache.Clear();
if (queryCaches.Count == 0)
{
Expand All @@ -1193,6 +1219,7 @@ public void EvictQueries(string cacheRegion)
{
if (settings.IsQueryCacheEnabled)
{
CheckNotClosed();
if (queryCaches.TryGetValue(cacheRegion, out var currentQueryCache))
{
currentQueryCache.Value.Clear();
Expand Down