public void NonDistributedInDoubt()
 {
     try
     {
         using (var scope = new TransactionScope())
         {
             _log.InfoFormat(
                 "Scope opened, id {0}, distributed id {1}",
                 SysTran.Current.TransactionInformation.LocalIdentifier,
                 SysTran.Current.TransactionInformation.DistributedIdentifier);
             // Simulate a simple connection: durable resource supporting single phase.
             // (Note that SQL Server 2005 and above use IPromotableSinglePhaseNotification
             // for delegating the resource management to the SQL server.)
             EnlistResource.EnlistInDoubtDurable();
             _log.InfoFormat(
                 "Fake connection opened, scope id {0} and distributed id {1}",
                 SysTran.Current.TransactionInformation.LocalIdentifier,
                 SysTran.Current.TransactionInformation.DistributedIdentifier);
             // Simulate NHibernate : volatile no single phase support + enlist in prepare option
             EnlistResource.EnlistWithPrepareEnlistmentVolatile();
             _log.InfoFormat(
                 "Fake session opened, scope id {0} and distributed id {1}",
                 SysTran.Current.TransactionInformation.LocalIdentifier,
                 SysTran.Current.TransactionInformation.DistributedIdentifier);
             scope.Complete();
             _log.Info("Scope completed");
         }
     }
     catch (TransactionInDoubtException)
     {
         // expected
     }
     _log.Info("Scope disposed");
 }
 public void NonDistributedNpgsqlCommit([Values(false, true)] bool enlistInPrepare)
 {
     using (var scope = new TransactionScope())
     {
         _log.InfoFormat(
             "Scope opened, id {0}, distributed id {1}",
             SysTran.Current.TransactionInformation.LocalIdentifier,
             SysTran.Current.TransactionInformation.DistributedIdentifier);
         // Simulate a Npgsql connection: as of Npgsql 3.2.4, volatile resource with single phase support
         EnlistResource.EnlistVolatile(false, true);
         _log.InfoFormat(
             "Fake connection opened, scope id {0} and distributed id {1}",
             SysTran.Current.TransactionInformation.LocalIdentifier,
             SysTran.Current.TransactionInformation.DistributedIdentifier);
         // Simulate NHibernate : volatile no single phase support
         if (enlistInPrepare)
         {
             EnlistResource.EnlistWithPrepareEnlistmentVolatile();
         }
         else
         {
             EnlistResource.EnlistVolatile();
         }
         _log.InfoFormat(
             "Fake session opened, scope id {0} and distributed id {1}",
             SysTran.Current.TransactionInformation.LocalIdentifier,
             SysTran.Current.TransactionInformation.DistributedIdentifier);
         scope.Complete();
         _log.Info("Scope completed");
     }
     _log.Info("Scope disposed");
 }
 public void NonDistributedCommit([Values(false, true)] bool enlistInPrepare)
 {
     using (var scope = new TransactionScope())
     {
         _log.InfoFormat(
             "Scope opened, id {0}, distributed id {1}",
             SysTran.Current.TransactionInformation.LocalIdentifier,
             SysTran.Current.TransactionInformation.DistributedIdentifier);
         // Simulate a simple connection: durable resource supporting single phase.
         // (Note that SQL Server 2005 and above use IPromotableSinglePhaseNotification
         // for delegating the resource management to the SQL server.)
         EnlistResource.EnlistDurable(false, true);
         _log.InfoFormat(
             "Fake connection opened, scope id {0} and distributed id {1}",
             SysTran.Current.TransactionInformation.LocalIdentifier,
             SysTran.Current.TransactionInformation.DistributedIdentifier);
         // Simulate NHibernate : volatile no single phase support
         if (enlistInPrepare)
         {
             EnlistResource.EnlistWithPrepareEnlistmentVolatile();
         }
         else
         {
             EnlistResource.EnlistVolatile();
         }
         _log.InfoFormat(
             "Fake session opened, scope id {0} and distributed id {1}",
             SysTran.Current.TransactionInformation.LocalIdentifier,
             SysTran.Current.TransactionInformation.DistributedIdentifier);
         scope.Complete();
         _log.Info("Scope completed");
     }
     _log.Info("Scope disposed");
 }
 public void DistributedFailureInSecondPhase()
 {
     using (var scope = new TransactionScope(TransactionScopeOption.Required, TimeSpan.FromSeconds(5)))
     {
         _log.InfoFormat(
             "Scope opened, id {0}, distributed id {1}",
             SysTran.Current.TransactionInformation.LocalIdentifier,
             SysTran.Current.TransactionInformation.DistributedIdentifier);
         // Simulate a failing connection
         EnlistResource.EnlistSecondPhaseFailingDurable();
         _log.InfoFormat(
             "Fake connection opened, scope id {0} and distributed id {1}",
             SysTran.Current.TransactionInformation.LocalIdentifier,
             SysTran.Current.TransactionInformation.DistributedIdentifier);
         // Simulate another resource, not even supporting single phase
         EnlistResource.EnlistDurable();
         _log.InfoFormat(
             "Fake other resource, scope id {0} and distributed id {1}",
             SysTran.Current.TransactionInformation.LocalIdentifier,
             SysTran.Current.TransactionInformation.DistributedIdentifier);
         // Simulate NHibernate : volatile no single phase support + enlist in prepare option
         EnlistResource.EnlistWithPrepareEnlistmentVolatile();
         _log.InfoFormat(
             "Fake session opened, scope id {0} and distributed id {1}",
             SysTran.Current.TransactionInformation.LocalIdentifier,
             SysTran.Current.TransactionInformation.DistributedIdentifier);
         scope.Complete();
         _log.Info("Scope completed");
     }
     _log.Info("Scope disposed");
 }
        public void DistributedRollback([Values(false, true)] bool fromConnection, [Values(false, true)] bool fromOther, [Values(false, true)] bool fromSession)
        {
            var shouldFail = fromConnection || fromSession || fromOther;

            try
            {
                using (var scope = new TransactionScope())
                {
                    _log.InfoFormat(
                        "Scope opened, id {0}, distributed id {1}",
                        SysTran.Current.TransactionInformation.LocalIdentifier,
                        SysTran.Current.TransactionInformation.DistributedIdentifier);
                    // Simulate a simple connection: durable resource supporting single phase.
                    // (Note that SQL Server 2005 and above use IPromotableSinglePhaseNotification
                    // for delegating the resource management to the SQL server.)
                    EnlistResource.EnlistDurable(fromConnection, true);
                    _log.InfoFormat(
                        "Fake connection opened, scope id {0} and distributed id {1}",
                        SysTran.Current.TransactionInformation.LocalIdentifier,
                        SysTran.Current.TransactionInformation.DistributedIdentifier);
                    // Simulate another resource, not even supporting single phase
                    EnlistResource.EnlistDurable(fromOther);
                    _log.InfoFormat(
                        "Fake other resource, scope id {0} and distributed id {1}",
                        SysTran.Current.TransactionInformation.LocalIdentifier,
                        SysTran.Current.TransactionInformation.DistributedIdentifier);
                    // Simulate NHibernate : volatile no single phase support + enlist in prepare option
                    EnlistResource.EnlistWithPrepareEnlistmentVolatile(fromSession);
                    _log.InfoFormat(
                        "Fake session opened, scope id {0} and distributed id {1}",
                        SysTran.Current.TransactionInformation.LocalIdentifier,
                        SysTran.Current.TransactionInformation.DistributedIdentifier);
                    if (shouldFail)
                    {
                        scope.Complete();
                        _log.Info("Scope completed");
                    }
                    else
                    {
                        _log.Info("Scope not completed for triggering rollback");
                    }
                }
            }
            catch (TransactionAbortedException)
            {
                if (!shouldFail)
                {
                    throw;
                }
            }
            _log.Info("Scope disposed");
        }
        public void DistributedNpgsqlRollback([Values(false, true)] bool fromConnection, [Values(false, true)] bool fromOther, [Values(false, true)] bool fromSession)
        {
            var shouldFail = fromConnection || fromSession || fromOther;

            try
            {
                using (var scope = new TransactionScope())
                {
                    _log.InfoFormat(
                        "Scope opened, id {0}, distributed id {1}",
                        SysTran.Current.TransactionInformation.LocalIdentifier,
                        SysTran.Current.TransactionInformation.DistributedIdentifier);
                    // Simulate a Npgsql connection: as of Npgsql 3.2.4, volatile resource with single phase support
                    EnlistResource.EnlistVolatile(fromConnection, true);
                    _log.InfoFormat(
                        "Fake connection opened, scope id {0} and distributed id {1}",
                        SysTran.Current.TransactionInformation.LocalIdentifier,
                        SysTran.Current.TransactionInformation.DistributedIdentifier);
                    // Simulate another resource, not even supporting single phase (required for going distributed with "Npgsql")
                    EnlistResource.EnlistDurable(fromOther);
                    _log.InfoFormat(
                        "Fake other resource, scope id {0} and distributed id {1}",
                        SysTran.Current.TransactionInformation.LocalIdentifier,
                        SysTran.Current.TransactionInformation.DistributedIdentifier);
                    // Simulate NHibernate : volatile no single phase support + enlist in prepare option
                    EnlistResource.EnlistWithPrepareEnlistmentVolatile(fromSession);
                    _log.InfoFormat(
                        "Fake session opened, scope id {0} and distributed id {1}",
                        SysTran.Current.TransactionInformation.LocalIdentifier,
                        SysTran.Current.TransactionInformation.DistributedIdentifier);
                    if (shouldFail)
                    {
                        scope.Complete();
                        _log.Info("Scope completed");
                    }
                    else
                    {
                        _log.Info("Scope not completed for triggering rollback");
                    }
                }
            }
            catch (TransactionAbortedException)
            {
                if (!shouldFail)
                {
                    throw;
                }
            }
            _log.Info("Scope disposed");
        }