|
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 |
|
@@ -522,6 +521,126 @@ public void AdditionalJoinDoesNotThrow()
|
522 | 521 | Assert.DoesNotThrow(() => s.JoinTransaction());
|
523 | 522 | }
|
524 | 523 | }
|
| 524 | + |
| 525 | + [Theory] |
| 526 | + public void CanUseDependentTransaction(bool explicitFlush) |
| 527 | + { |
| 528 | + if (!TestDialect.SupportsDependentTransaction) |
| 529 | + Assert.Ignore("Dialect does not support dependent transactions"); |
| 530 | + IgnoreIfUnsupported(explicitFlush); |
| 531 | + |
| 532 | + try |
| 533 | + { |
| 534 | + using (var committable = new CommittableTransaction()) |
| 535 | + { |
| 536 | + System.Transactions.Transaction.Current = committable; |
| 537 | + using (var clone = committable.DependentClone(DependentCloneOption.RollbackIfNotComplete)) |
| 538 | + { |
| 539 | + System.Transactions.Transaction.Current = clone; |
| 540 | + |
| 541 | + using (var s = OpenSession()) |
| 542 | + { |
| 543 | + if (!AutoJoinTransaction) |
| 544 | + s.JoinTransaction(); |
| 545 | + s.Save(new Person()); |
| 546 | + |
| 547 | + if (explicitFlush) |
| 548 | + s.Flush(); |
| 549 | + clone.Complete(); |
| 550 | + } |
| 551 | + } |
| 552 | + |
| 553 | + System.Transactions.Transaction.Current = committable; |
| 554 | + committable.Commit(); |
| 555 | + } |
| 556 | + } |
| 557 | + finally |
| 558 | + { |
| 559 | + System.Transactions.Transaction.Current = null; |
| 560 | + } |
| 561 | + } |
| 562 | + |
| 563 | + [Theory] |
| 564 | + public void CanUseSessionWithManyDependentTransaction(bool explicitFlush) |
| 565 | + { |
| 566 | + if (!TestDialect.SupportsDependentTransaction) |
| 567 | + Assert.Ignore("Dialect does not support dependent transactions"); |
| 568 | + IgnoreIfUnsupported(explicitFlush); |
| 569 | + // ODBC with SQL-Server always causes system transactions to go distributed, which causes their transaction completion to run |
| 570 | + // asynchronously. But ODBC enlistment also check the previous transaction in a way that do not guard against it |
| 571 | + // being concurrently disposed of. See https://github.com/nhibernate/nhibernate-core/pull/1505 for more details. |
| 572 | + if (Sfi.ConnectionProvider.Driver is OdbcDriver) |
| 573 | + Assert.Ignore("ODBC sometimes fails on second scope by checking the previous transaction status, which may yield an object disposed exception"); |
| 574 | + // SAP HANA & SQL Anywhere .Net providers always cause system transactions to be distributed, causing them to |
| 575 | + // complete on concurrent threads. This creates race conditions when chaining scopes, the subsequent scope usage |
| 576 | + // finding the connection still enlisted in the previous transaction, its complete being still not finished |
| 577 | + // on its own thread. |
| 578 | + if (Sfi.ConnectionProvider.Driver is HanaDriverBase || Sfi.ConnectionProvider.Driver is SapSQLAnywhere17Driver) |
| 579 | + Assert.Ignore("SAP HANA and SQL Anywhere scope handling causes concurrency issues preventing chaining scope usages."); |
| 580 | + |
| 581 | + try |
| 582 | + { |
| 583 | + using (var s = WithOptions().ConnectionReleaseMode(ConnectionReleaseMode.OnClose).OpenSession()) |
| 584 | + { |
| 585 | + using (var committable = new CommittableTransaction()) |
| 586 | + { |
| 587 | + System.Transactions.Transaction.Current = committable; |
| 588 | + using (var clone = committable.DependentClone(DependentCloneOption.RollbackIfNotComplete)) |
| 589 | + { |
| 590 | + System.Transactions.Transaction.Current = clone; |
| 591 | + if (!AutoJoinTransaction) |
| 592 | + s.JoinTransaction(); |
| 593 | + // Acquire the connection |
| 594 | + var count = s.Query<Person>().Count(); |
| 595 | + Assert.That(count, Is.EqualTo(0), "Unexpected initial entity count."); |
| 596 | + clone.Complete(); |
| 597 | + } |
| 598 | + |
| 599 | + using (var clone = committable.DependentClone(DependentCloneOption.RollbackIfNotComplete)) |
| 600 | + { |
| 601 | + System.Transactions.Transaction.Current = clone; |
| 602 | + if (!AutoJoinTransaction) |
| 603 | + s.JoinTransaction(); |
| 604 | + s.Save(new Person()); |
| 605 | + |
| 606 | + if (explicitFlush) |
| 607 | + s.Flush(); |
| 608 | + |
| 609 | + clone.Complete(); |
| 610 | + } |
| 611 | + |
| 612 | + using (var clone = committable.DependentClone(DependentCloneOption.RollbackIfNotComplete)) |
| 613 | + { |
| 614 | + System.Transactions.Transaction.Current = clone; |
| 615 | + if (!AutoJoinTransaction) |
| 616 | + s.JoinTransaction(); |
| 617 | + var count = s.Query<Person>().Count(); |
| 618 | + Assert.That(count, Is.EqualTo(1), "Unexpected entity count after committed insert."); |
| 619 | + clone.Complete(); |
| 620 | + } |
| 621 | + |
| 622 | + System.Transactions.Transaction.Current = committable; |
| 623 | + committable.Commit(); |
| 624 | + } |
| 625 | + } |
| 626 | + } |
| 627 | + finally |
| 628 | + { |
| 629 | + System.Transactions.Transaction.Current = null; |
| 630 | + } |
| 631 | + |
| 632 | + using (var s = OpenSession()) |
| 633 | + { |
| 634 | + using (var tx = new TransactionScope()) |
| 635 | + { |
| 636 | + if (!AutoJoinTransaction) |
| 637 | + s.JoinTransaction(); |
| 638 | + var count = s.Query<Person>().Count(); |
| 639 | + Assert.That(count, Is.EqualTo(1), "Unexpected entity count after global commit."); |
| 640 | + tx.Complete(); |
| 641 | + } |
| 642 | + } |
| 643 | + } |
525 | 644 | }
|
526 | 645 |
|
527 | 646 | [TestFixture]
|
|
0 commit comments