Skip to content

Commit 99f96f5

Browse files
NH-2176 - More tests. Preparing PostgreSql case.
1 parent 88bc587 commit 99f96f5

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed

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

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,35 @@ public void CanRollbackTransaction()
177177
AssertNoPersons();
178178
}
179179

180+
[Test]
181+
public void CanRollbackTransactionFromScope()
182+
{
183+
using (new TransactionScope())
184+
using (var s = sessions.OpenSession())
185+
{
186+
ForceEscalationToDistributedTx.Escalate();
187+
s.Save(new Person { CreatedAt = DateTime.Today });
188+
// No Complete call for triggering rollback.
189+
}
190+
191+
AssertNoPersons();
192+
}
193+
194+
[Test]
195+
public void CanRollbackTransactionFromScopeWithFlush()
196+
{
197+
using (new TransactionScope())
198+
using (var s = sessions.OpenSession())
199+
{
200+
ForceEscalationToDistributedTx.Escalate();
201+
s.Save(new Person { CreatedAt = DateTime.Today });
202+
s.Flush();
203+
// No Complete call for triggering rollback.
204+
}
205+
206+
AssertNoPersons();
207+
}
208+
180209
[Test]
181210
public void CanRollbackTransactionWithFlush()
182211
{
@@ -270,6 +299,43 @@ public void RollbackOutsideNhWithFlush()
270299
AssertNoPersons();
271300
}
272301

302+
[Test]
303+
[Description("rollback inside nh-session-scope should not commit save and the transaction should be aborted.")]
304+
public void TransactionInsertWithRollBackFromScope()
305+
{
306+
using (new TransactionScope())
307+
{
308+
using (var s = sessions.OpenSession())
309+
{
310+
var person = new Person { CreatedAt = DateTime.Now };
311+
s.Save(person);
312+
ForceEscalationToDistributedTx.Escalate();
313+
person.CreatedAt = DateTime.Now;
314+
}
315+
// No Complete call for triggering rollback.
316+
}
317+
AssertNoPersons();
318+
}
319+
320+
[Test]
321+
[Description("rollback inside nh-session-scope should not commit save and the transaction should be aborted.")]
322+
public void TransactionInsertWithRollBackFromScopeWithFlush()
323+
{
324+
using (new TransactionScope())
325+
{
326+
using (var s = sessions.OpenSession())
327+
{
328+
var person = new Person { CreatedAt = DateTime.Now };
329+
s.Save(person);
330+
ForceEscalationToDistributedTx.Escalate();
331+
person.CreatedAt = DateTime.Now;
332+
s.Flush();
333+
}
334+
// No Complete call for triggering rollback.
335+
}
336+
AssertNoPersons();
337+
}
338+
273339
[Test]
274340
[Description("rollback inside nh-session-scope should not commit save and the transaction should be aborted.")]
275341
public void TransactionInsertWithRollBackTask()
@@ -330,6 +396,43 @@ public void TransactionInsertWithRollBackTaskWithFlush()
330396
[Test]
331397
[Description(@"Two session in two txscope
332398
(without an explicit NH transaction and without an explicit flush)
399+
and with a rollback in the second dtc and a rollback outside nh-session-scope.")]
400+
public void TransactionInsertLoadWithRollBackFromScope()
401+
{
402+
object savedId;
403+
var createdAt = DateTime.Today;
404+
using (var txscope = new TransactionScope())
405+
{
406+
using (var s = sessions.OpenSession())
407+
{
408+
var person = new Person { CreatedAt = createdAt };
409+
savedId = s.Save(person);
410+
}
411+
txscope.Complete();
412+
}
413+
414+
using (new TransactionScope())
415+
{
416+
using (var s = sessions.OpenSession())
417+
{
418+
var person = s.Get<Person>(savedId);
419+
person.CreatedAt = createdAt.AddMonths(-1);
420+
}
421+
ForceEscalationToDistributedTx.Escalate();
422+
423+
// No Complete call for triggering rollback.
424+
}
425+
426+
using (var s = sessions.OpenSession())
427+
using (s.BeginTransaction())
428+
{
429+
Assert.AreEqual(createdAt, s.Get<Person>(savedId).CreatedAt, "Entity update was not rollback-ed.");
430+
}
431+
}
432+
433+
[Test]
434+
[Description(@"Two session in two txscope
435+
(without an explicit NH transaction and without an explicit flush)
333436
and with a rollback in the second dtc and a ForceRollback outside nh-session-scope.")]
334437
public void TransactionInsertLoadWithRollBackTask()
335438
{
@@ -374,6 +477,45 @@ public void TransactionInsertLoadWithRollBackTask()
374477
}
375478
}
376479

480+
[Test]
481+
[Description(@"Two session in two txscope
482+
(without an explicit NH transaction and without an explicit flush)
483+
and with a rollback in the second dtc and a rollback outside nh-session-scope.")]
484+
public void TransactionInsertLoadFlushWithRollBackFromScope()
485+
{
486+
object savedId;
487+
var createdAt = DateTime.Today;
488+
using (var txscope = new TransactionScope())
489+
{
490+
using (var s = sessions.OpenSession())
491+
{
492+
var person = new Person { CreatedAt = createdAt };
493+
savedId = s.Save(person);
494+
s.Flush();
495+
}
496+
txscope.Complete();
497+
}
498+
499+
using (new TransactionScope())
500+
{
501+
using (var s = sessions.OpenSession())
502+
{
503+
var person = s.Get<Person>(savedId);
504+
person.CreatedAt = createdAt.AddMonths(-1);
505+
s.Flush();
506+
}
507+
ForceEscalationToDistributedTx.Escalate();
508+
509+
// No Complete call for triggering rollback.
510+
}
511+
512+
using (var s = sessions.OpenSession())
513+
using (s.BeginTransaction())
514+
{
515+
Assert.AreEqual(createdAt, s.Get<Person>(savedId).CreatedAt, "Entity update was not rollback-ed.");
516+
}
517+
}
518+
377519
[Test]
378520
[Description(@"Two session in two txscope
379521
(without an explicit NH transaction and with an explicit flush)

src/NHibernate/Driver/NpgsqlDriver.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ protected override bool SupportsPreparingCommands
6666
get { return true; }
6767
}
6868

69+
public override bool SupportsNullEnlistment => false;
70+
6971
public override IResultSetsCommand GetResultSetsCommand(Engine.ISessionImplementor session)
7072
{
7173
return new BasicResultSetsCommand(session);

0 commit comments

Comments
 (0)