Skip to content

Commit 419d434

Browse files
NH-2176 - Moving "DtcFailures" to system transactions tests, and doing complete non distributed tests.
1 parent b867d25 commit 419d434

File tree

11 files changed

+458
-115
lines changed

11 files changed

+458
-115
lines changed

src/NHibernate.Test/NHSpecificTest/DtcFailures/Person.cs

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,6 +1448,9 @@
14481448
<Compile Include="Stateless\TreeNode.cs" />
14491449
<Compile Include="Subselect\ClassSubselectFixture.cs" />
14501450
<Compile Include="Subselect\Domain.cs" />
1451+
<Compile Include="SystemTransactions\DistributedTransactionFixture.cs" />
1452+
<Compile Include="SystemTransactions\Person.cs" />
1453+
<Compile Include="SystemTransactions\TransactionFixtureBase.cs" />
14511454
<Compile Include="TestCaseMappingByCode.cs" />
14521455
<Compile Include="TestDialect.cs" />
14531456
<Compile Include="TestDialects\MsSql2008TestDialect.cs" />
@@ -1594,8 +1597,6 @@
15941597
<Compile Include="NHSpecificTest\NH1574\StatelessTest.cs" />
15951598
<Compile Include="NHSpecificTest\NH1574\Team.cs" />
15961599
<Compile Include="NHSpecificTest\Dates\TimeFixture.cs" />
1597-
<Compile Include="NHSpecificTest\DtcFailures\DtcFailuresFixture.cs" />
1598-
<Compile Include="NHSpecificTest\DtcFailures\Person.cs" />
15991600
<Compile Include="NHSpecificTest\Futures\FallbackFixture.cs" />
16001601
<Compile Include="NHSpecificTest\Futures\FutureFixture.cs" />
16011602
<Compile Include="NHSpecificTest\Logs\LogsFixture.cs" />
@@ -3282,6 +3283,7 @@
32823283
<EmbeddedResource Include="NHSpecificTest\NH1291AnonExample\Mappings.hbm.xml" />
32833284
</ItemGroup>
32843285
<ItemGroup>
3286+
<EmbeddedResource Include="SystemTransactions\Person.hbm.xml" />
32853287
<EmbeddedResource Include="SessionBuilder\Mappings.hbm.xml" />
32863288
<EmbeddedResource Include="IdTest\IdentityClass.hbm.xml" />
32873289
<EmbeddedResource Include="NHSpecificTest\NH1904\StructMappings.hbm.xml" />
@@ -3753,7 +3755,6 @@
37533755
<EmbeddedResource Include="NHSpecificTest\NH1710\Defined.hbm.xml" />
37543756
<EmbeddedResource Include="NHSpecificTest\NH1713\Mappings.hbm.xml" />
37553757
<EmbeddedResource Include="NHSpecificTest\NH1715\Mappings.hbm.xml" />
3756-
<EmbeddedResource Include="NHSpecificTest\DtcFailures\Mappings.hbm.xml" />
37573758
<EmbeddedResource Include="NHSpecificTest\NH1694\Mappings.hbm.xml" />
37583759
<EmbeddedResource Include="NHSpecificTest\NH1706\Mappings.hbm.xml" />
37593760
<EmbeddedResource Include="Stateless\Naturalness.hbm.xml" />

src/NHibernate.Test/NHSpecificTest/DtcFailures/DtcFailuresFixture.cs renamed to src/NHibernate.Test/SystemTransactions/DistributedTransactionFixture.cs

Lines changed: 73 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,26 @@
11
using System;
2-
using System.Collections;
32
using System.Linq;
4-
using System.Reflection;
53
using System.Threading;
64
using System.Transactions;
75
using log4net;
86
using log4net.Repository.Hierarchy;
9-
using NHibernate.Cfg;
10-
using NHibernate.Cfg.MappingSchema;
117
using NHibernate.Linq;
12-
using NHibernate.Tool.hbm2ddl;
138
using NUnit.Framework;
149

15-
namespace NHibernate.Test.NHSpecificTest.DtcFailures
10+
namespace NHibernate.Test.SystemTransactions
1611
{
1712
[TestFixture]
18-
public class DtcFailuresFixture : TestCase
13+
public class DistributedTransactionFixture : TransactionFixtureBase
1914
{
20-
private static readonly ILog _log = LogManager.GetLogger(typeof(DtcFailuresFixture));
21-
22-
protected override IList Mappings
23-
=> new[] { "NHSpecificTest.DtcFailures.Mappings.hbm.xml" };
24-
25-
protected override string MappingsAssembly
26-
=> "NHibernate.Test";
15+
private static readonly ILog _log = LogManager.GetLogger(typeof(DistributedTransactionFixture));
2716

2817
protected override bool AppliesTo(Dialect.Dialect dialect)
2918
=> dialect.SupportsDistributedTransactions;
3019

31-
protected override void CreateSchema()
32-
{
33-
// Copied from Configure method.
34-
var config = new Configuration();
35-
if (TestConfigurationHelper.hibernateConfigFile != null)
36-
config.Configure(TestConfigurationHelper.hibernateConfigFile);
37-
38-
// Our override so we can set nullability on database column without NHibernate knowing about it.
39-
config.BeforeBindMapping += BeforeBindMapping;
40-
41-
// Copied from AddMappings methods.
42-
var assembly = Assembly.Load(MappingsAssembly);
43-
foreach (var file in Mappings)
44-
config.AddResource(MappingsAssembly + "." + file, assembly);
45-
46-
// Copied from CreateSchema method, but we use our own config.
47-
new SchemaExport(config).Create(false, true);
48-
}
49-
5020
protected override void OnTearDown()
5121
{
5222
DodgeTransactionCompletionDelayIfRequired();
53-
54-
using (var s = OpenSession())
55-
using (var t = s.BeginTransaction())
56-
{
57-
s.CreateQuery("delete from System.Object").ExecuteUpdate();
58-
t.Commit();
59-
}
60-
}
61-
62-
private void BeforeBindMapping(object sender, BindMappingEventArgs e)
63-
{
64-
var prop = e.Mapping.RootClasses[0].Properties.OfType<HbmProperty>().Single(p => p.Name == "NotNullData");
65-
prop.notnull = true;
66-
prop.notnullSpecified = true;
23+
base.OnTearDown();
6724
}
6825

6926
[Test]
@@ -108,7 +65,7 @@ public void SupportsPromotingToDistributed()
10865
}
10966

11067
[Test]
111-
public void WillNotCrashOnDtcPrepareFailure()
68+
public void WillNotCrashOnPrepareFailure()
11269
{
11370
var tx = new TransactionScope();
11471
var disposeCalled = false;
@@ -484,6 +441,74 @@ public void NH1744()
484441
}
485442
}
486443

444+
[Test]
445+
public void CanUseSessionWithManyScopes([Values(false, true)] bool explicitFlush)
446+
{
447+
// Note that this fails with ConnectionReleaseMode.OnClose and SqlServer:
448+
// System.Data.SqlClient.SqlException : Microsoft Distributed Transaction Coordinator (MS DTC) has stopped this transaction.
449+
using (var s = OpenSession())
450+
//using (var s = Sfi.WithOptions().ConnectionReleaseMode(ConnectionReleaseMode.OnClose).OpenSession())
451+
{
452+
using (var tx = new TransactionScope())
453+
{
454+
ForceEscalationToDistributedTx.Escalate();
455+
// Acquire the connection
456+
var count = s.Query<Person>().Count();
457+
Assert.That(count, Is.EqualTo(0), "Unexpected initial entity count.");
458+
tx.Complete();
459+
}
460+
// No dodge here please! Allow to check chaining usages do not fail.
461+
using (var tx = new TransactionScope())
462+
{
463+
s.Save(new Person { CreatedAt = DateTime.Today });
464+
465+
ForceEscalationToDistributedTx.Escalate();
466+
467+
if (explicitFlush)
468+
s.Flush();
469+
470+
tx.Complete();
471+
}
472+
473+
DodgeTransactionCompletionDelayIfRequired();
474+
475+
using (var tx = new TransactionScope())
476+
{
477+
ForceEscalationToDistributedTx.Escalate();
478+
var count = s.Query<Person>().Count();
479+
Assert.That(count, Is.EqualTo(1), "Unexpected entity count after committed insert.");
480+
tx.Complete();
481+
}
482+
using (new TransactionScope())
483+
{
484+
s.Save(new Person { CreatedAt = DateTime.Today });
485+
486+
ForceEscalationToDistributedTx.Escalate();
487+
488+
if (explicitFlush)
489+
s.Flush();
490+
491+
// No complete for rollback-ing.
492+
}
493+
494+
DodgeTransactionCompletionDelayIfRequired();
495+
496+
// Do not reuse the session after a rollback, its state does not allow it.
497+
// http://nhibernate.info/doc/nhibernate-reference/manipulatingdata.html#manipulatingdata-endingsession-commit
498+
}
499+
500+
using (var s = OpenSession())
501+
{
502+
using (var tx = new TransactionScope())
503+
{
504+
ForceEscalationToDistributedTx.Escalate();
505+
var count = s.Query<Person>().Count();
506+
Assert.That(count, Is.EqualTo(1), "Unexpected entity count after rollback-ed insert.");
507+
tx.Complete();
508+
}
509+
}
510+
}
511+
487512
[Test]
488513
public void CanUseSessionOutsideOfScopeAfterScope([Values(false, true)] bool explicitFlush)
489514
{
@@ -551,15 +576,6 @@ public void DelayedTransactionCompletion([Values(false, true)] bool explicitFlus
551576
}
552577
}
553578

554-
private void AssertNoPersons()
555-
{
556-
using (var s = OpenSession())
557-
using (s.BeginTransaction())
558-
{
559-
Assert.AreEqual(0, s.Query<Person>().Count(), "Entities found in database.");
560-
}
561-
}
562-
563579
private void DodgeTransactionCompletionDelayIfRequired()
564580
{
565581
if (Sfi.ConnectionProvider.Driver.HasDelayedDistributedTransactionCompletion)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
3+
namespace NHibernate.Test.SystemTransactions
4+
{
5+
public class Person
6+
{
7+
public virtual DateTime CreatedAt { get; set; }
8+
9+
public virtual int Id { get; set; }
10+
11+
public virtual string NotNullData { get; set; } = "not-null";
12+
}
13+
}

src/NHibernate.Test/NHSpecificTest/DtcFailures/Mappings.hbm.xml renamed to src/NHibernate.Test/SystemTransactions/Person.hbm.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8" ?>
22
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
3-
namespace="NHibernate.Test.NHSpecificTest.DtcFailures"
3+
namespace="NHibernate.Test.SystemTransactions"
44
assembly="NHibernate.Test">
55

66
<class name="Person">

0 commit comments

Comments
 (0)