Skip to content

Commit aea3577

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

File tree

11 files changed

+457
-120
lines changed

11 files changed

+457
-120
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: 72 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,22 @@
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-
50-
protected override void OnTearDown()
51-
{
52-
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;
67-
}
68-
6920
[Test]
7021
public void SupportsEnlistingInDistributed()
7122
{
@@ -108,7 +59,7 @@ public void SupportsPromotingToDistributed()
10859
}
10960

11061
[Test]
111-
public void WillNotCrashOnDtcPrepareFailure()
62+
public void WillNotCrashOnPrepareFailure()
11263
{
11364
var tx = new TransactionScope();
11465
var disposeCalled = false;
@@ -484,6 +435,74 @@ public void NH1744()
484435
}
485436
}
486437

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

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-
563573
private void DodgeTransactionCompletionDelayIfRequired()
564574
{
565575
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)