Skip to content

Commit 4558d53

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

File tree

5 files changed

+66
-1
lines changed

5 files changed

+66
-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: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
[Test]
29+
public void GetChildSession_ShouldReturnNonClosedInstance()
30+
{
31+
using (var rootSession = OpenSession())
32+
{
33+
var childSession1 = rootSession.GetChildSession();
34+
childSession1.Close();
35+
36+
using (var childSession2 = rootSession.GetChildSession())
37+
{
38+
Assert.DoesNotThrow(() => { childSession2.Get<Process>(Guid.NewGuid()); });
39+
}
40+
}
41+
}
42+
}
43+
}
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: 3 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>

src/NHibernate/Impl/SessionImpl.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public sealed class SessionImpl : AbstractSessionImpl, IEventSource, ISerializab
7373
private readonly StatefulPersistenceContext persistenceContext;
7474

7575
[NonSerialized]
76-
private readonly ISession rootSession;
76+
private readonly SessionImpl rootSession;
7777

7878
[NonSerialized]
7979
ISession _childSession;
@@ -381,6 +381,7 @@ public DbConnection Close()
381381
{
382382
SetClosed();
383383
Cleanup();
384+
if (rootSession != null) rootSession._childSession = null;
384385
}
385386
}
386387
}
@@ -1681,6 +1682,7 @@ private void Dispose(bool isDisposing)
16811682
// free unmanaged resources here
16821683

16831684
IsAlreadyDisposed = true;
1685+
16841686
// nothing for Finalizer to do - so tell the GC to ignore it
16851687
GC.SuppressFinalize(this);
16861688
}

0 commit comments

Comments
 (0)