|
6 | 6 | using NHibernate.Cfg;
|
7 | 7 | using NHibernate.Driver;
|
8 | 8 | using NHibernate.Engine;
|
9 |
| -using NHibernate.Linq; |
10 | 9 | using NHibernate.Test.TransactionTest;
|
11 | 10 | using NUnit.Framework;
|
12 | 11 |
|
@@ -509,6 +508,120 @@ public void AdditionalJoinDoesNotThrow()
|
509 | 508 | Assert.DoesNotThrow(() => s.JoinTransaction());
|
510 | 509 | }
|
511 | 510 | }
|
| 511 | + |
| 512 | + [Theory] |
| 513 | + public void CanUseDependentTransaction(bool explicitFlush) |
| 514 | + { |
| 515 | + if (!TestDialect.SupportsDependentTransaction) |
| 516 | + Assert.Ignore("Dialect does not support dependent transactions"); |
| 517 | + IgnoreIfUnsupported(explicitFlush); |
| 518 | + |
| 519 | + try |
| 520 | + { |
| 521 | + using (var committable = new CommittableTransaction()) |
| 522 | + { |
| 523 | + System.Transactions.Transaction.Current = committable; |
| 524 | + using (var clone = committable.DependentClone(DependentCloneOption.RollbackIfNotComplete)) |
| 525 | + { |
| 526 | + System.Transactions.Transaction.Current = clone; |
| 527 | + |
| 528 | + using (var s = OpenSession()) |
| 529 | + { |
| 530 | + if (!AutoJoinTransaction) |
| 531 | + s.JoinTransaction(); |
| 532 | + s.Save(new Person()); |
| 533 | + |
| 534 | + if (explicitFlush) |
| 535 | + s.Flush(); |
| 536 | + clone.Complete(); |
| 537 | + } |
| 538 | + } |
| 539 | + |
| 540 | + System.Transactions.Transaction.Current = committable; |
| 541 | + committable.Commit(); |
| 542 | + } |
| 543 | + } |
| 544 | + finally |
| 545 | + { |
| 546 | + System.Transactions.Transaction.Current = null; |
| 547 | + } |
| 548 | + } |
| 549 | + |
| 550 | + [Theory] |
| 551 | + public void CanUseSessionWithManyDependentTransaction(bool explicitFlush) |
| 552 | + { |
| 553 | + if (!TestDialect.SupportsDependentTransaction) |
| 554 | + Assert.Ignore("Dialect does not support dependent transactions"); |
| 555 | + IgnoreIfUnsupported(explicitFlush); |
| 556 | + // ODBC with SQL-Server always causes system transactions to go distributed, which causes their transaction completion to run |
| 557 | + // asynchronously. But ODBC enlistment also check the previous transaction in a way that do not guard against it |
| 558 | + // being concurrently disposed of. See https://github.com/nhibernate/nhibernate-core/pull/1505 for more details. |
| 559 | + if (Sfi.ConnectionProvider.Driver is OdbcDriver) |
| 560 | + Assert.Ignore("ODBC sometimes fails on second scope by checking the previous transaction status, which may yield an object disposed exception"); |
| 561 | + |
| 562 | + try |
| 563 | + { |
| 564 | + using (var s = WithOptions().ConnectionReleaseMode(ConnectionReleaseMode.OnClose).OpenSession()) |
| 565 | + { |
| 566 | + using (var committable = new CommittableTransaction()) |
| 567 | + { |
| 568 | + System.Transactions.Transaction.Current = committable; |
| 569 | + using (var clone = committable.DependentClone(DependentCloneOption.RollbackIfNotComplete)) |
| 570 | + { |
| 571 | + System.Transactions.Transaction.Current = clone; |
| 572 | + if (!AutoJoinTransaction) |
| 573 | + s.JoinTransaction(); |
| 574 | + // Acquire the connection |
| 575 | + var count = s.Query<Person>().Count(); |
| 576 | + Assert.That(count, Is.EqualTo(0), "Unexpected initial entity count."); |
| 577 | + clone.Complete(); |
| 578 | + } |
| 579 | + |
| 580 | + using (var clone = committable.DependentClone(DependentCloneOption.RollbackIfNotComplete)) |
| 581 | + { |
| 582 | + System.Transactions.Transaction.Current = clone; |
| 583 | + if (!AutoJoinTransaction) |
| 584 | + s.JoinTransaction(); |
| 585 | + s.Save(new Person()); |
| 586 | + |
| 587 | + if (explicitFlush) |
| 588 | + s.Flush(); |
| 589 | + |
| 590 | + clone.Complete(); |
| 591 | + } |
| 592 | + |
| 593 | + using (var clone = committable.DependentClone(DependentCloneOption.RollbackIfNotComplete)) |
| 594 | + { |
| 595 | + System.Transactions.Transaction.Current = clone; |
| 596 | + if (!AutoJoinTransaction) |
| 597 | + s.JoinTransaction(); |
| 598 | + var count = s.Query<Person>().Count(); |
| 599 | + Assert.That(count, Is.EqualTo(1), "Unexpected entity count after committed insert."); |
| 600 | + clone.Complete(); |
| 601 | + } |
| 602 | + |
| 603 | + System.Transactions.Transaction.Current = committable; |
| 604 | + committable.Commit(); |
| 605 | + } |
| 606 | + } |
| 607 | + } |
| 608 | + finally |
| 609 | + { |
| 610 | + System.Transactions.Transaction.Current = null; |
| 611 | + } |
| 612 | + |
| 613 | + using (var s = OpenSession()) |
| 614 | + { |
| 615 | + using (var tx = new TransactionScope()) |
| 616 | + { |
| 617 | + if (!AutoJoinTransaction) |
| 618 | + s.JoinTransaction(); |
| 619 | + var count = s.Query<Person>().Count(); |
| 620 | + Assert.That(count, Is.EqualTo(1), "Unexpected entity count after global commit."); |
| 621 | + tx.Complete(); |
| 622 | + } |
| 623 | + } |
| 624 | + } |
512 | 625 | }
|
513 | 626 |
|
514 | 627 | [TestFixture]
|
|
0 commit comments