Skip to content

Commit 68e06d4

Browse files
alobakovhazzik
authored andcommitted
NH-3985 - Fix for subsequent child sessions being returned in disposed state
1 parent 2d2095b commit 68e06d4

File tree

5 files changed

+62
-1
lines changed

5 files changed

+62
-1
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
3+
namespace NHibernate.Test.NHSpecificTest.NH3985
4+
{
5+
public partial class Process
6+
{
7+
public virtual Guid ProcessID { get; set; }
8+
public virtual string Name { get; set; }
9+
}
10+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using NUnit.Framework;
2+
using System;
3+
4+
namespace NHibernate.Test.NHSpecificTest.NH3985
5+
{
6+
/// <summary>
7+
/// The test verifies that subsequent child sessions are not issued in already-disposed state.
8+
/// </summary>
9+
[TestFixture]
10+
public class Fixture : BugTestCase
11+
{
12+
[Test]
13+
public void GetChildSession_ShouldReturnNonDisposedInstance()
14+
{
15+
using (var rootSession = OpenSession())
16+
{
17+
using (var childSession1 = rootSession.GetChildSession())
18+
{
19+
}
20+
21+
using (var childSession2 = rootSession.GetChildSession())
22+
{
23+
Assert.DoesNotThrow(() => { childSession2.Get<Process>(Guid.NewGuid()); });
24+
}
25+
}
26+
}
27+
}
28+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test" namespace="NHibernate.Test.NHSpecificTest.NH3985">
3+
<class name="Process">
4+
<id name="ProcessID" generator="guid.comb" />
5+
<property name="Name" />
6+
</class>
7+
</hibernate-mapping>

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,8 @@
743743
<Compile Include="NHSpecificTest\EntityWithUserTypeCanHaveLinqGenerators\IExample.cs" />
744744
<Compile Include="Insertordering\NH3931Entities.cs" />
745745
<Compile Include="NHSpecificTest\NH1904\StructFixture.cs" />
746+
<Compile Include="NHSpecificTest\NH3985\Entity.cs" />
747+
<Compile Include="NHSpecificTest\NH3985\Fixture.cs" />
746748
<Compile Include="NHSpecificTest\NH3247\Entity.cs" />
747749
<Compile Include="NHSpecificTest\NH3247\Fixture.cs" />
748750
<Compile Include="NHSpecificTest\NH3386\Entity.cs" />
@@ -2668,6 +2670,7 @@
26682670
<EmbeddedResource Include="NHSpecificTest\NH2195\Mappings.hbm.xml" />
26692671
<EmbeddedResource Include="NHSpecificTest\NH3187\Mappings.hbm.xml" />
26702672
<EmbeddedResource Include="NHSpecificTest\NH3932\Model\Mappings.hbm.xml" />
2673+
<EmbeddedResource Include="NHSpecificTest\NH3985\Mappings.hbm.xml" />
26712674
<EmbeddedResource Include="TypedManyToOne\Customer.hbm.xml" />
26722675
</ItemGroup>
26732676
<ItemGroup>
@@ -3253,6 +3256,10 @@
32533256
</ItemGroup>
32543257
<ItemGroup>
32553258
<EmbeddedResource Include="NHSpecificTest\NH1904\StructMappings.hbm.xml" />
3259+
<EmbeddedResource Include="NHSpecificTest\NH3985\Mappings.hbm.xml">
3260+
<SubType>Designer</SubType>
3261+
</EmbeddedResource>
3262+
<EmbeddedResource Include="NHSpecificTest\NH3985\Mappings.hbm.xml" />
32563263
<EmbeddedResource Include="Insertordering\FamilyModel\Mappings.hbm.xml" />
32573264
<EmbeddedResource Include="Insertordering\AnimalModel\Mappings.hbm.xml" />
32583265
<EmbeddedResource Include="NHSpecificTest\NH3247\Mappings.hbm.xml" />

src/NHibernate/Impl/SessionImpl.cs

Lines changed: 10 additions & 1 deletion
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
}
@@ -2604,5 +2608,10 @@ public override IEntityPersister GetEntityPersister(string entityName, object ob
26042608
}
26052609
}
26062610
}
2611+
2612+
void RemoveChildSession(ISession session)
2613+
{
2614+
Interlocked.CompareExchange(ref _childSession, null, session);
2615+
}
26072616
}
26082617
}

0 commit comments

Comments
 (0)