public void Rollback(SinglePhaseEnlistment singlePhaseEnlistment)
        {
            // prevent commands in main thread to run concurrently
            Driver driver = connection.driver;

            lock (driver)
            {
                rollbackThreadId = Thread.CurrentThread.ManagedThreadId;
                while (connection.Reader != null)
                {
                    // wait for reader to finish. Maybe we should not wait
                    // forever and cancel it after some time?
                    System.Threading.Thread.Sleep(100);
                }
                simpleTransaction.Rollback();
                singlePhaseEnlistment.Aborted();
                DriverTransactionManager.RemoveDriverInTransaction(baseTransaction);

                driver.CurrentTransaction = null;

                if (connection.State == ConnectionState.Closed)
                {
                    connection.CloseFully();
                }
                rollbackThreadId = 0;
            }
        }
        /// <summary>
        /// Enlists in the specified transaction.
        /// </summary>
        /// <param name="transaction">
        /// A reference to an existing <see cref="System.Transactions.Transaction"/> in which to enlist.
        /// </param>
        public override void EnlistTransaction(Transaction transaction)
        {
            // enlisting in the null transaction is a noop
            if (transaction == null)
            {
                return;
            }

            // guard against trying to enlist in more than one transaction
            if (driver.CurrentTransaction != null)
            {
                if (driver.CurrentTransaction.BaseTransaction == transaction)
                {
                    return;
                }

                Throw(new MyCatException("Already enlisted"));
            }

            // now see if we need to swap out drivers.  We would need to do this since
            // we have to make sure all ops for a given transaction are done on the
            // same physical connection.
            Driver existingDriver = DriverTransactionManager.GetDriverInTransaction(transaction);

            if (existingDriver != null)
            {
                // we can't allow more than one driver to contribute to the same connection
                if (existingDriver.IsInActiveUse)
                {
                    Throw(new NotSupportedException(Resources.MultipleConnectionsInTransactionNotSupported));
                }

                // there is an existing driver and it's not being currently used.
                // now we need to see if it is using the same connection string
                string text1 = existingDriver.Settings.ConnectionString;
                string text2 = Settings.ConnectionString;
                if (String.Compare(text1, text2, true) != 0)
                {
                    Throw(new NotSupportedException(Resources.MultipleConnectionsInTransactionNotSupported));
                }

                // close existing driver
                // set this new driver as our existing driver
                CloseFully();
                driver = existingDriver;
            }

            if (driver.CurrentTransaction == null)
            {
                MyCatPromotableTransaction t = new MyCatPromotableTransaction(this, transaction);
                if (!transaction.EnlistPromotableSinglePhase(t))
                {
                    Throw(new NotSupportedException(Resources.DistributedTxnNotSupported));
                }

                driver.CurrentTransaction = t;
                DriverTransactionManager.SetDriverInTransaction(driver);
                driver.IsInActiveUse = true;
            }
        }
        public void SinglePhaseCommit(SinglePhaseEnlistment singlePhaseEnlistment)
        {
            simpleTransaction.Commit();
            singlePhaseEnlistment.Committed();
            DriverTransactionManager.RemoveDriverInTransaction(baseTransaction);
            connection.driver.CurrentTransaction = null;

            if (connection.State == ConnectionState.Closed)
            {
                connection.CloseFully();
            }
        }
        /// <include file='docs/MyCatConnection.xml' path='docs/Open/*'/>
        public override void Open()
        {
            if (State == ConnectionState.Open)
            {
                Throw(new InvalidOperationException(Resources.ConnectionAlreadyOpen));
            }

#if !NETSTANDARD1_3
            // start up our interceptors
            exceptionInterceptor = new ExceptionInterceptor(this);
            commandInterceptor   = new CommandInterceptor(this);
#endif

            SetState(ConnectionState.Connecting, true);

            AssertPermissions();

#if !NETSTANDARD1_3
            // if we are auto enlisting in a current transaction, then we will be
            // treating the connection as pooled
            if (Settings.AutoEnlist && Transaction.Current != null)
            {
                driver = DriverTransactionManager.GetDriverInTransaction(Transaction.Current);
                if (driver != null &&
                    (driver.IsInActiveUse ||
                     !driver.Settings.EquivalentTo(this.Settings)))
                {
                    Throw(new NotSupportedException(Resources.MultipleConnectionsInTransactionNotSupported));
                }
            }
#endif

            try
            {
                MyCatConnectionStringBuilder currentSettings = Settings;
#if SUPPORT_REPLICATION
                // Load balancing
                if (ReplicationManager.IsReplicationGroup(Settings.Server))
                {
                    if (driver == null)
                    {
                        ReplicationManager.GetNewConnection(Settings.Server, false, this);
                    }
                    else
                    {
                        currentSettings = driver.Settings;
                    }
                }
#endif
#if !NETSTANDARD1_3
                if (Settings.Pooling)
                {
                    MyCatPool pool = MyCatPoolManager.GetPool(currentSettings);
                    if (driver == null || !driver.IsOpen)
                    {
                        driver = pool.GetConnection();
                    }
                    procedureCache = pool.ProcedureCache;
                }
                else
                {
                    if (driver == null || !driver.IsOpen)
                    {
                        driver = Driver.Create(currentSettings);
                    }
                    procedureCache = new ProcedureCache((int)Settings.ProcedureCacheSize);
                }
#else
                if (driver == null || !driver.IsOpen)
                {
                    driver = Driver.Create(currentSettings);
                }
                procedureCache = new ProcedureCache((int)Settings.ProcedureCacheSize);
#endif
            }
            catch (Exception ex)
            {
                SetState(ConnectionState.Closed, true);
                throw ex;
            }

            SetState(ConnectionState.Open, false);
            driver.Configure(this);

            if (!(driver.SupportsPasswordExpiration && driver.IsPasswordExpired))
            {
                if (Settings.Database != null && Settings.Database != String.Empty)
                {
                    ChangeDatabase(Settings.Database);
                }
            }

            // setup our schema provider
            schemaProvider = new ISSchemaProvider(this);
            perfMonitor    = new PerformanceMonitor(this);

            // if we are opening up inside a current transaction, then autoenlist
            // TODO: control this with a connection string option
#if !NETSTANDARD1_3
            if (Transaction.Current != null && Settings.AutoEnlist)
            {
                EnlistTransaction(Transaction.Current);
            }
#endif

            hasBeenOpen = true;
            SetState(ConnectionState.Open, true);
        }