Skip to content

Commit 390011b

Browse files
committed
unset child session as soon as it is disposed.
1 parent 9b39a2a commit 390011b

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

src/NHibernate.Test/NHSpecificTest/NH3985/Fixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public void GetChildSession_ShouldReturnNonDisposedInstance()
2020

2121
using (var childSession2 = rootSession.GetChildSession())
2222
{
23-
Assert.DoesNotThrow(new TestDelegate(() => { childSession2.Get<Process>(Guid.NewGuid()); }));
23+
Assert.DoesNotThrow(() => { childSession2.Get<Process>(Guid.NewGuid()); });
2424
}
2525
}
2626
}

src/NHibernate/Impl/SessionImpl.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Linq.Expressions;
77
using System.Runtime.Serialization;
88
using System.Security;
9+
using System.Threading;
910
using NHibernate.AdoNet;
1011
using NHibernate.Collection;
1112
using NHibernate.Criterion;
@@ -73,7 +74,7 @@ public sealed class SessionImpl : AbstractSessionImpl, IEventSource, ISerializab
7374
private readonly StatefulPersistenceContext persistenceContext;
7475

7576
[NonSerialized]
76-
private readonly ISession rootSession;
77+
private readonly SessionImpl rootSession;
7778

7879
[NonSerialized]
7980
ISession _childSession;
@@ -1681,6 +1682,9 @@ private void Dispose(bool isDisposing)
16811682
// free unmanaged resources here
16821683

16831684
IsAlreadyDisposed = true;
1685+
1686+
rootSession?.RemoveChildSession(this);
1687+
16841688
// nothing for Finalizer to do - so tell the GC to ignore it
16851689
GC.SuppressFinalize(this);
16861690
}
@@ -2221,10 +2225,8 @@ public ISession GetChildSession()
22212225

22222226
CheckAndUpdateSessionStatus();
22232227

2224-
var childImpl = _childSession as SessionImpl;
2225-
22262228
// if child session never existed or has already been disposed, create new
2227-
if (childImpl == null || childImpl.IsAlreadyDisposed)
2229+
if (_childSession == null)
22282230
{
22292231
log.Debug("Creating child session.");
22302232
_childSession = new SessionImpl(this);
@@ -2607,5 +2609,10 @@ public override IEntityPersister GetEntityPersister(string entityName, object ob
26072609
}
26082610
}
26092611
}
2612+
2613+
void RemoveChildSession(ISession session)
2614+
{
2615+
Interlocked.CompareExchange(ref _childSession, null, session);
2616+
}
26102617
}
26112618
}

0 commit comments

Comments
 (0)