4
4
using System . Transactions ;
5
5
using log4net ;
6
6
using log4net . Repository . Hierarchy ;
7
+ using NHibernate . Cfg ;
8
+ using NHibernate . Engine ;
7
9
using NHibernate . Linq ;
8
10
using NHibernate . Test . TransactionTest ;
9
11
using NUnit . Framework ;
@@ -15,6 +17,7 @@ public class DistributedSystemTransactionFixture : SystemTransactionFixtureBase
15
17
{
16
18
private static readonly ILog _log = LogManager . GetLogger ( typeof ( DistributedSystemTransactionFixture ) ) ;
17
19
protected override bool UseConnectionOnSystemTransactionPrepare => true ;
20
+ protected override bool AutoJoinTransaction => true ;
18
21
19
22
protected override bool AppliesTo ( Dialect . Dialect dialect )
20
23
=> dialect . SupportsDistributedTransactions && base . AppliesTo ( dialect ) ;
@@ -463,6 +466,8 @@ public void CanUseSessionWithManyScopes(bool explicitFlush)
463
466
using ( var tx = new TransactionScope ( ) )
464
467
{
465
468
ForceEscalationToDistributedTx . Escalate ( ) ;
469
+ if ( ! AutoJoinTransaction )
470
+ s . JoinTransaction ( ) ;
466
471
// Acquire the connection
467
472
var count = s . Query < Person > ( ) . Count ( ) ;
468
473
Assert . That ( count , Is . EqualTo ( 0 ) , "Unexpected initial entity count." ) ;
@@ -471,6 +476,8 @@ public void CanUseSessionWithManyScopes(bool explicitFlush)
471
476
// No dodge here please! Allow to check chaining usages do not fail.
472
477
using ( var tx = new TransactionScope ( ) )
473
478
{
479
+ if ( ! AutoJoinTransaction )
480
+ s . JoinTransaction ( ) ;
474
481
s . Save ( new Person ( ) ) ;
475
482
476
483
ForceEscalationToDistributedTx . Escalate ( ) ;
@@ -486,12 +493,16 @@ public void CanUseSessionWithManyScopes(bool explicitFlush)
486
493
using ( var tx = new TransactionScope ( ) )
487
494
{
488
495
ForceEscalationToDistributedTx . Escalate ( ) ;
496
+ if ( ! AutoJoinTransaction )
497
+ s . JoinTransaction ( ) ;
489
498
var count = s . Query < Person > ( ) . Count ( ) ;
490
499
Assert . That ( count , Is . EqualTo ( 1 ) , "Unexpected entity count after committed insert." ) ;
491
500
tx . Complete ( ) ;
492
501
}
493
502
using ( new TransactionScope ( ) )
494
503
{
504
+ if ( ! AutoJoinTransaction )
505
+ s . JoinTransaction ( ) ;
495
506
s . Save ( new Person ( ) ) ;
496
507
497
508
ForceEscalationToDistributedTx . Escalate ( ) ;
@@ -513,6 +524,8 @@ public void CanUseSessionWithManyScopes(bool explicitFlush)
513
524
using ( var tx = new TransactionScope ( ) )
514
525
{
515
526
ForceEscalationToDistributedTx . Escalate ( ) ;
527
+ if ( ! AutoJoinTransaction )
528
+ s . JoinTransaction ( ) ;
516
529
var count = s . Query < Person > ( ) . Count ( ) ;
517
530
Assert . That ( count , Is . EqualTo ( 1 ) , "Unexpected entity count after rollback-ed insert." ) ;
518
531
tx . Complete ( ) ;
@@ -528,10 +541,12 @@ public void CanUseSessionOutsideOfScopeAfterScope(bool explicitFlush)
528
541
// NpgsqlOperationInProgressException: The connection is already in state 'Executing'
529
542
// Not much an issue since it is advised to not use ConnectionReleaseMode.OnClose.
530
543
using ( var s = OpenSession ( ) )
531
- //using (var s = Sfi. WithOptions().ConnectionReleaseMode(ConnectionReleaseMode.OnClose).OpenSession())
544
+ //using (var s = WithOptions().ConnectionReleaseMode(ConnectionReleaseMode.OnClose).OpenSession())
532
545
{
533
546
using ( var tx = new TransactionScope ( ) )
534
547
{
548
+ if ( ! AutoJoinTransaction )
549
+ s . JoinTransaction ( ) ;
535
550
s . Save ( new Person ( ) ) ;
536
551
537
552
ForceEscalationToDistributedTx . Escalate ( ) ;
@@ -611,7 +626,7 @@ public void WhenCommittingItemsAfterSessionDisposalWillAddThemTo2ndLevelCache()
611
626
const string notNullData = "test" ;
612
627
using ( var tx = new TransactionScope ( ) )
613
628
{
614
- using ( var s = Sfi . OpenSession ( ) )
629
+ using ( var s = OpenSession ( ) )
615
630
{
616
631
var person = new CacheablePerson { NotNullData = notNullData } ;
617
632
s . Save ( person ) ;
@@ -646,7 +661,7 @@ public void WhenCommittingItemsAfterSessionDisposalWillAddThemTo2ndLevelCache()
646
661
// entity to load, allowing the session to not use the connection at all.
647
662
// Will fail if a transaction manager tries to enlist user supplied connection. Do
648
663
// not add a transaction scope below.
649
- using ( var s = Sfi . WithOptions ( ) . Connection ( connection ) . OpenSession ( ) )
664
+ using ( var s = WithOptions ( ) . Connection ( connection ) . OpenSession ( ) )
650
665
{
651
666
CacheablePerson person = null ;
652
667
Assert . DoesNotThrow ( ( ) => person = s . Load < CacheablePerson > ( id ) , "Failed loading entity from second level cache." ) ;
@@ -662,6 +677,8 @@ public void DoNotDeadlockOnAfterTransactionWait()
662
677
using ( var tx = new TransactionScope ( ) )
663
678
{
664
679
ForceEscalationToDistributedTx . Escalate ( ) ;
680
+ if ( ! AutoJoinTransaction )
681
+ s . JoinTransaction ( ) ;
665
682
s . Save ( new Person ( ) ) ;
666
683
667
684
s . Flush ( ) ;
@@ -680,6 +697,8 @@ public void EnforceConnectionUsageRulesOnTransactionCompletion()
680
697
using ( var tx = new TransactionScope ( ) )
681
698
{
682
699
ForceEscalationToDistributedTx . Escalate ( ) ;
700
+ if ( ! AutoJoinTransaction )
701
+ s . JoinTransaction ( ) ;
683
702
s . Save ( new Person ( ) ) ;
684
703
685
704
s . Flush ( ) ;
@@ -698,6 +717,17 @@ public void EnforceConnectionUsageRulesOnTransactionCompletion()
698
717
Assert . That ( interceptor . AfterException , Is . TypeOf < HibernateException > ( ) ) ;
699
718
}
700
719
720
+ [ Test ]
721
+ public void AdditionalJoinDoesNotThrow ( )
722
+ {
723
+ using ( new TransactionScope ( ) )
724
+ using ( var s = OpenSession ( ) )
725
+ {
726
+ ForceEscalationToDistributedTx . Escalate ( ) ;
727
+ Assert . DoesNotThrow ( ( ) => s . JoinTransaction ( ) ) ;
728
+ }
729
+ }
730
+
701
731
private void DodgeTransactionCompletionDelayIfRequired ( )
702
732
{
703
733
if ( Sfi . ConnectionProvider . Driver . HasDelayedDistributedTransactionCompletion )
@@ -760,4 +790,32 @@ public class DistributedSystemTransactionWithoutConnectionFromPrepareFixture : D
760
790
{
761
791
protected override bool UseConnectionOnSystemTransactionPrepare => false ;
762
792
}
793
+
794
+ public class DistributedSystemTransactionWithoutAutoJoinTransaction : DistributedSystemTransactionFixture
795
+ {
796
+ protected override bool AutoJoinTransaction => false ;
797
+
798
+ protected override void Configure ( Configuration configuration )
799
+ {
800
+ base . Configure ( configuration ) ;
801
+ DisableConnectionAutoEnlist ( configuration ) ;
802
+ }
803
+
804
+ protected override bool AppliesTo ( ISessionFactoryImplementor factory )
805
+ => base . AppliesTo ( factory ) && factory . ConnectionProvider . Driver . SupportsEnlistmentWhenAutoEnlistmentIsDisabled ;
806
+
807
+ [ Test ]
808
+ public void SessionIsNotEnlisted ( )
809
+ {
810
+ using ( new TransactionScope ( ) )
811
+ {
812
+ ForceEscalationToDistributedTx . Escalate ( ) ;
813
+ // Dodge the OpenSession override which call JoinTransaction by calling WithOptions().
814
+ using ( var s = WithOptions ( ) . OpenSession ( ) )
815
+ {
816
+ Assert . That ( s . GetSessionImplementation ( ) . TransactionContext , Is . Null ) ;
817
+ }
818
+ }
819
+ }
820
+ }
763
821
}
0 commit comments