Skip to content

Commit 47c986b

Browse files
authored
Merge branch 'master' into NH-3944
2 parents 78548d4 + 928a2eb commit 47c986b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1757
-322
lines changed

src/NHibernate.Test/DebugSessionFactory.cs

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Concurrent;
33
using System.Collections.Generic;
44
using System.Data.Common;
5+
using System.Linq;
56
using System.Threading;
67
using log4net;
78
using NHibernate.Cache;
@@ -53,29 +54,41 @@ public bool CheckSessionsWereClosed()
5354
var allClosed = true;
5455
foreach (var session in _openedSessions)
5556
{
56-
if (session.IsOpen)
57+
// Do not inverse, we want to close all of them.
58+
allClosed = CheckSessionWasClosed(session) && allClosed;
59+
// Catches only session opened from another one while sharing the connection. Those
60+
// opened without sharing the connection stay un-monitored.
61+
foreach (var dependentSession in session.ConnectionManager.DependentSessions.ToList())
5762
{
58-
if (session.TransactionContext?.ShouldCloseSessionOnDistributedTransactionCompleted ?? false)
59-
{
60-
// Delayed transactions not having completed and closed their sessions? Give them a chance to complete.
61-
Thread.Sleep(100);
62-
if (!session.IsOpen)
63-
{
64-
_log.Warn($"Test case had a delayed close of session {session.SessionId}.");
65-
continue;
66-
}
67-
}
68-
69-
_log.Error($"Test case didn't close session {session.SessionId}, closing");
70-
allClosed = false;
71-
(session as ISession)?.Close();
72-
(session as IStatelessSession)?.Close();
63+
allClosed = CheckSessionWasClosed(dependentSession) && allClosed;
7364
}
7465
}
7566

7667
return allClosed;
7768
}
7869

70+
private bool CheckSessionWasClosed(ISessionImplementor session)
71+
{
72+
if (!session.IsOpen)
73+
return true;
74+
75+
if (session.TransactionContext?.ShouldCloseSessionOnDistributedTransactionCompleted ?? false)
76+
{
77+
// Delayed transactions not having completed and closed their sessions? Give them a chance to complete.
78+
Thread.Sleep(100);
79+
if (!session.IsOpen)
80+
{
81+
_log.Warn($"Test case had a delayed close of session {session.SessionId}.");
82+
return true;
83+
}
84+
}
85+
86+
_log.Error($"Test case didn't close session {session.SessionId}, closing");
87+
(session as ISession)?.Close();
88+
(session as IStatelessSession)?.Close();
89+
return false;
90+
}
91+
7992
ISessionBuilder ISessionFactory.WithOptions()
8093
{
8194
return new SessionBuilder(ActualFactory.WithOptions(), this);

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,6 +1467,8 @@
14671467
<Compile Include="TestDialects\PostgreSQL83TestDialect.cs" />
14681468
<Compile Include="TestDialects\SQLiteTestDialect.cs" />
14691469
<Compile Include="Tools\hbm2ddl\SchemaExportTests\ExportToFileFixture.cs" />
1470+
<Compile Include="TransactionTest\Person.cs" />
1471+
<Compile Include="TransactionTest\TransactionFixtureBase.cs" />
14701472
<Compile Include="TransformTests\ImplementationOfEqualityTests.cs" />
14711473
<Compile Include="TypesTest\CharClass.cs" />
14721474
<Compile Include="TypesTest\CharClassFixture.cs" />
@@ -3289,6 +3291,7 @@
32893291
<EmbeddedResource Include="NHSpecificTest\NH1291AnonExample\Mappings.hbm.xml" />
32903292
</ItemGroup>
32913293
<ItemGroup>
3294+
<EmbeddedResource Include="TransactionTest\Person.hbm.xml" />
32923295
<EmbeddedResource Include="NHSpecificTest\NH2241\Mappings.hbm.xml" />
32933296
<EmbeddedResource Include="Futures\Mappings.hbm.xml" />
32943297
<EmbeddedResource Include="SessionBuilder\Mappings.hbm.xml" />
Lines changed: 81 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
1-
using System.Collections;
1+
using System.Collections.Generic;
2+
using System.Linq;
23
using System.Transactions;
3-
using NHibernate.DomainModel;
4+
using NHibernate.Linq;
5+
using NHibernate.Test.TransactionTest;
46
using NUnit.Framework;
57

68
namespace NHibernate.Test.SystemTransactions
79
{
810
[TestFixture]
9-
public class TransactionFixture : TestCase
11+
public class TransactionFixture : TransactionFixtureBase
1012
{
11-
protected override IList Mappings
12-
{
13-
get { return new string[] { "WZ.hbm.xml" }; }
14-
}
15-
1613
[Test]
1714
public void CanUseSystemTransactionsToCommit()
1815
{
19-
object identifier;
16+
int identifier;
2017
using(ISession session = Sfi.OpenSession())
2118
using(TransactionScope tx = new TransactionScope())
2219
{
23-
W s = new W();
20+
var s = new Person();
2421
session.Save(s);
2522
identifier = s.Id;
2623
tx.Complete();
@@ -29,11 +26,84 @@ public void CanUseSystemTransactionsToCommit()
2926
using (ISession session = Sfi.OpenSession())
3027
using (TransactionScope tx = new TransactionScope())
3128
{
32-
W w = session.Get<W>(identifier);
29+
var w = session.Get<Person>(identifier);
3330
Assert.IsNotNull(w);
3431
session.Delete(w);
3532
tx.Complete();
3633
}
3734
}
35+
36+
[Test]
37+
public void FlushFromTransactionAppliesToDisposedSharingSession()
38+
{
39+
var flushOrder = new List<int>();
40+
using (var s = OpenSession(new TestInterceptor(0, flushOrder)))
41+
{
42+
var builder = s.SessionWithOptions().Connection();
43+
44+
using (var t = new TransactionScope())
45+
{
46+
var p1 = new Person();
47+
var p2 = new Person();
48+
var p3 = new Person();
49+
var p4 = new Person();
50+
51+
using (var s1 = builder.Interceptor(new TestInterceptor(1, flushOrder)).OpenSession())
52+
s1.Save(p1);
53+
using (var s2 = builder.Interceptor(new TestInterceptor(2, flushOrder)).OpenSession())
54+
{
55+
s2.Save(p2);
56+
using (var s3 = s2.SessionWithOptions().Connection().Interceptor(new TestInterceptor(3, flushOrder)).OpenSession())
57+
s3.Save(p3);
58+
}
59+
s.Save(p4);
60+
t.Complete();
61+
}
62+
}
63+
64+
Assert.That(flushOrder, Is.EqualTo(new[] { 0, 1, 2, 3 }));
65+
66+
using (var s = OpenSession())
67+
using (var t = s.BeginTransaction())
68+
{
69+
Assert.That(s.Query<Person>().Count(), Is.EqualTo(4));
70+
t.Commit();
71+
}
72+
}
73+
74+
[Test]
75+
public void FlushFromTransactionAppliesToSharingSession()
76+
{
77+
var flushOrder = new List<int>();
78+
using (var s = OpenSession(new TestInterceptor(0, flushOrder)))
79+
{
80+
var builder = s.SessionWithOptions().Connection();
81+
82+
using (var s1 = builder.Interceptor(new TestInterceptor(1, flushOrder)).OpenSession())
83+
using (var s2 = builder.Interceptor(new TestInterceptor(2, flushOrder)).OpenSession())
84+
using (var s3 = s2.SessionWithOptions().Connection().Interceptor(new TestInterceptor(3, flushOrder)).OpenSession())
85+
using (var t = new TransactionScope())
86+
{
87+
var p1 = new Person();
88+
var p2 = new Person();
89+
var p3 = new Person();
90+
var p4 = new Person();
91+
s1.Save(p1);
92+
s2.Save(p2);
93+
s3.Save(p3);
94+
s.Save(p4);
95+
t.Complete();
96+
}
97+
}
98+
99+
Assert.That(flushOrder, Is.EqualTo(new[] { 0, 1, 2, 3 }));
100+
101+
using (var s = OpenSession())
102+
using (var t = s.BeginTransaction())
103+
{
104+
Assert.That(s.Query<Person>().Count(), Is.EqualTo(4));
105+
t.Commit();
106+
}
107+
}
38108
}
39109
}

src/NHibernate.Test/TestCase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ protected virtual void CreateSchema()
276276

277277
protected virtual void DropSchema()
278278
{
279-
if (Sfi.ConnectionProvider.Driver is FirebirdClientDriver fbDriver)
279+
if (Sfi?.ConnectionProvider.Driver is FirebirdClientDriver fbDriver)
280280
{
281281
// Firebird will pool each connection created during the test and will marked as used any table
282282
// referenced by queries. It will at best delays those tables drop until connections are actually

src/NHibernate.Test/Tools/hbm2ddl/SchemaMetadataUpdaterTest/HeavyEntity.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ public class Order
77
public string And { get; set; }
88
public string Column { get; set; }
99
public string Name { get; set; }
10+
public string Abracadabra { get; set; }
1011
}
1112
}

src/NHibernate.Test/Tools/hbm2ddl/SchemaMetadataUpdaterTest/HeavyEntity.hbm.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<property name="And"/>
1414
<property name="Column"/>
1515
<property name="Name"/>
16+
<property name="Abracadabra"/>
1617
</class>
1718

1819
</hibernate-mapping>

0 commit comments

Comments
 (0)