Skip to content

Commit fc1c256

Browse files
committed
NH-3985 - Fix for subsequent child sessions being returned in disposed state
1 parent db526d7 commit fc1c256

File tree

5 files changed

+89
-1
lines changed

5 files changed

+89
-1
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
3+
namespace NHibernate.Test.NHSpecificTest.NH3985
4+
{
5+
public partial class Process
6+
{
7+
#region Extensibility Method Definitions
8+
9+
public override bool Equals(object obj)
10+
{
11+
Process toCompare = obj as Process;
12+
if (toCompare == null)
13+
{
14+
return false;
15+
}
16+
17+
if (Object.Equals(this.ProcessID, default(long)) &&
18+
Object.Equals(toCompare.ProcessID, default(long)))
19+
return ReferenceEquals(this, toCompare);
20+
21+
if (!Object.Equals(this.ProcessID, toCompare.ProcessID))
22+
return false;
23+
24+
return true;
25+
}
26+
27+
public override int GetHashCode()
28+
{
29+
int hashCode = 13;
30+
31+
// on transient objects, use the basic GetHashCode()
32+
if (Object.Equals(this.ProcessID, default(long)))
33+
return base.GetHashCode();
34+
35+
hashCode = (hashCode * 7) + ProcessID.GetHashCode();
36+
return hashCode;
37+
}
38+
39+
#endregion
40+
41+
public virtual Guid ProcessID { get; set; }
42+
43+
public virtual string Name { get; set; }
44+
}
45+
}
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(new TestDelegate(() => { 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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,8 @@
741741
<Compile Include="NHSpecificTest\EntityWithUserTypeCanHaveLinqGenerators\FooExample.cs" />
742742
<Compile Include="NHSpecificTest\EntityWithUserTypeCanHaveLinqGenerators\IExample.cs" />
743743
<Compile Include="Insertordering\NH3931Entities.cs" />
744+
<Compile Include="NHSpecificTest\NH3985\Entity.cs" />
745+
<Compile Include="NHSpecificTest\NH3985\Fixture.cs" />
744746
<Compile Include="NHSpecificTest\NH3247\Entity.cs" />
745747
<Compile Include="NHSpecificTest\NH3247\Fixture.cs" />
746748
<Compile Include="NHSpecificTest\NH3386\Entity.cs" />
@@ -3248,6 +3250,9 @@
32483250
<EmbeddedResource Include="NHSpecificTest\NH1291AnonExample\Mappings.hbm.xml" />
32493251
</ItemGroup>
32503252
<ItemGroup>
3253+
<EmbeddedResource Include="NHSpecificTest\NH3985\Mappings.hbm.xml">
3254+
<SubType>Designer</SubType>
3255+
</EmbeddedResource>
32513256
<EmbeddedResource Include="Insertordering\FamilyModel\Mappings.hbm.xml" />
32523257
<EmbeddedResource Include="Insertordering\AnimalModel\Mappings.hbm.xml" />
32533258
<EmbeddedResource Include="NHSpecificTest\NH3247\Mappings.hbm.xml" />

src/NHibernate/Impl/SessionImpl.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2221,7 +2221,10 @@ public ISession GetChildSession()
22212221

22222222
CheckAndUpdateSessionStatus();
22232223

2224-
if (_childSession == null)
2224+
var childImpl = _childSession as SessionImpl;
2225+
2226+
// if child session never existed or has already been disposed, create new
2227+
if (childImpl == null || childImpl.IsAlreadyDisposed)
22252228
{
22262229
log.Debug("Creating child session.");
22272230
_childSession = new SessionImpl(this);

0 commit comments

Comments
 (0)