Skip to content

Commit 5760e16

Browse files
NH-2176 - added test variants with flushes. Temporarily enabling dtc for all to check if some db better handles dtc when there are explicit flushes.
1 parent 33e71cb commit 5760e16

File tree

1 file changed

+154
-1
lines changed

1 file changed

+154
-1
lines changed

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

Lines changed: 154 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ protected override string MappingsAssembly
2626
=> "NHibernate.Test";
2727

2828
protected override bool AppliesTo(Dialect.Dialect dialect)
29-
=> dialect.SupportsDistributedTransactions;
29+
=> true || dialect.SupportsDistributedTransactions;
3030

3131
protected override void CreateSchema()
3232
{
@@ -176,6 +176,42 @@ public void CanRollbackTransaction()
176176
AssertNoPersons();
177177
}
178178

179+
[Test]
180+
public void CanRollbackTransactionWithFlush()
181+
{
182+
var tx = new TransactionScope();
183+
var disposeCalled = false;
184+
try
185+
{
186+
using (var s = sessions.OpenSession())
187+
{
188+
ForceEscalationToDistributedTx.Escalate(true); //will rollback tx
189+
s.Save(new Person { CreatedAt = DateTime.Today });
190+
s.Flush();
191+
tx.Complete();
192+
}
193+
disposeCalled = true;
194+
Assert.Throws<TransactionAbortedException>(tx.Dispose, "Scope disposal has not rollback and throw.");
195+
}
196+
finally
197+
{
198+
if (!disposeCalled)
199+
{
200+
try
201+
{
202+
tx.Dispose();
203+
}
204+
catch
205+
{
206+
// Ignore, if disposed has not been called, another exception has occurred in the try and
207+
// we should avoid overriding it by the disposal failure.
208+
}
209+
}
210+
}
211+
212+
AssertNoPersons();
213+
}
214+
179215
[Test]
180216
[Description("Another action inside the transaction do the rollBack outside nh-session-scope.")]
181217
public void RollbackOutsideNh()
@@ -204,6 +240,35 @@ public void RollbackOutsideNh()
204240
AssertNoPersons();
205241
}
206242

243+
[Test]
244+
[Description("Another action inside the transaction do the rollBack outside nh-session-scope.")]
245+
public void RollbackOutsideNhWithFlush()
246+
{
247+
try
248+
{
249+
using (var txscope = new TransactionScope())
250+
{
251+
using (var s = sessions.OpenSession())
252+
{
253+
var person = new Person { CreatedAt = DateTime.Now };
254+
s.Save(person);
255+
s.Flush();
256+
}
257+
ForceEscalationToDistributedTx.Escalate(true); //will rollback tx
258+
259+
txscope.Complete();
260+
}
261+
262+
Assert.Fail("Scope disposal has not rollback and throw.");
263+
}
264+
catch (TransactionAbortedException)
265+
{
266+
_log.Debug("Transaction aborted.");
267+
}
268+
269+
AssertNoPersons();
270+
}
271+
207272
[Test]
208273
[Description("rollback inside nh-session-scope should not commit save and the transaction should be aborted.")]
209274
public void TransactionInsertWithRollBackTask()
@@ -232,6 +297,35 @@ public void TransactionInsertWithRollBackTask()
232297
AssertNoPersons();
233298
}
234299

300+
[Test]
301+
[Description("rollback inside nh-session-scope should not commit save and the transaction should be aborted.")]
302+
public void TransactionInsertWithRollBackTaskWithFlush()
303+
{
304+
try
305+
{
306+
using (var txscope = 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(true); //will rollback tx
313+
person.CreatedAt = DateTime.Now;
314+
s.Flush();
315+
}
316+
txscope.Complete();
317+
}
318+
319+
Assert.Fail("Scope disposal has not rollback and throw.");
320+
}
321+
catch (TransactionAbortedException)
322+
{
323+
_log.Debug("Transaction aborted.");
324+
}
325+
326+
AssertNoPersons();
327+
}
328+
235329
[Test]
236330
[Description(@"Two session in two txscope
237331
(without an explicit NH transaction and without an explicit flush)
@@ -405,6 +499,46 @@ public void CanDeleteItemInDtc()
405499
AssertNoPersons();
406500
}
407501

502+
[Test]
503+
public void CanDeleteItemInDtcWithFlush()
504+
{
505+
object id;
506+
using (var tx = new TransactionScope())
507+
{
508+
using (var s = sessions.OpenSession())
509+
{
510+
id = s.Save(new Person { CreatedAt = DateTime.Today });
511+
512+
ForceEscalationToDistributedTx.Escalate();
513+
514+
s.Flush();
515+
516+
tx.Complete();
517+
}
518+
}
519+
520+
using (var s = sessions.OpenSession())
521+
using (s.BeginTransaction())
522+
{
523+
Assert.AreEqual(1, s.Query<Person>().Count(), "Entity not found in database.");
524+
}
525+
526+
using (var tx = new TransactionScope())
527+
{
528+
using (var s = sessions.OpenSession())
529+
{
530+
ForceEscalationToDistributedTx.Escalate();
531+
532+
s.Delete(s.Get<Person>(id));
533+
s.Flush();
534+
535+
tx.Complete();
536+
}
537+
}
538+
539+
AssertNoPersons();
540+
}
541+
408542
[Test]
409543
[Description("Open/Close a session inside a TransactionScope fails.")]
410544
public void NH1744()
@@ -443,6 +577,25 @@ public void CanUseSessionOutsideOfScopeAfterScope()
443577
}
444578
}
445579

580+
[Test]
581+
public void CanUseSessionOutsideOfScopeAfterScopeWithFlush()
582+
{
583+
using (var s = sessions.WithOptions().ConnectionReleaseMode(ConnectionReleaseMode.OnClose).OpenSession())
584+
{
585+
using (var tx = new TransactionScope())
586+
{
587+
s.Save(new Person { CreatedAt = DateTime.Today });
588+
589+
ForceEscalationToDistributedTx.Escalate();
590+
591+
s.Flush();
592+
tx.Complete();
593+
}
594+
595+
Assert.AreEqual(1, s.Query<Person>().Count(), "Entity not found in database.");
596+
}
597+
}
598+
446599
private void AssertNoPersons()
447600
{
448601
using (var s = sessions.OpenSession())

0 commit comments

Comments
 (0)