Esempio n. 1
0
 private void Initialize()
 {
     if (!this.initialized)
     {
         OletxInternalResourceManager internalResourceManager = this.oletxTm.internalResourceManager;
         IntPtr zero = IntPtr.Zero;
         IResourceManagerShim resourceManagerShim = null;
         bool            nodeNameMatches          = false;
         CoTaskMemHandle whereaboutsBuffer        = null;
         RuntimeHelpers.PrepareConstrainedRegions();
         try
         {
             zero = HandleTable.AllocHandle(internalResourceManager);
             this.proxyShimFactory.ConnectToProxy(this.nodeName, internalResourceManager.Identifier, zero, out nodeNameMatches, out this.whereaboutsSize, out whereaboutsBuffer, out resourceManagerShim);
             if (!nodeNameMatches)
             {
                 throw new NotSupportedException(System.Transactions.SR.GetString("ProxyCannotSupportMultipleNodeNames"));
             }
             if ((whereaboutsBuffer != null) && (this.whereaboutsSize != 0))
             {
                 this.whereabouts = new byte[this.whereaboutsSize];
                 Marshal.Copy(whereaboutsBuffer.DangerousGetHandle(), this.whereabouts, 0, Convert.ToInt32(this.whereaboutsSize));
             }
             internalResourceManager.resourceManagerShim = resourceManagerShim;
             internalResourceManager.CallReenlistComplete();
             this.initialized = true;
         }
         catch (COMException exception)
         {
             if (System.Transactions.Oletx.NativeMethods.XACT_E_NOTSUPPORTED == exception.ErrorCode)
             {
                 throw new NotSupportedException(System.Transactions.SR.GetString("CannotSupportNodeNameSpecification"));
             }
             OletxTransactionManager.ProxyException(exception);
             throw TransactionManagerCommunicationException.Create(System.Transactions.SR.GetString("TraceSourceOletx"), System.Transactions.SR.GetString("TransactionManagerCommunicationException"), exception);
         }
         finally
         {
             if (whereaboutsBuffer != null)
             {
                 whereaboutsBuffer.Close();
             }
             if (!this.initialized)
             {
                 if ((zero != IntPtr.Zero) && (resourceManagerShim == null))
                 {
                     HandleTable.FreeHandle(zero);
                 }
                 if (this.whereabouts != null)
                 {
                     this.whereabouts     = null;
                     this.whereaboutsSize = 0;
                 }
             }
         }
     }
 }
Esempio n. 2
0
        internal OletxCommittableTransaction CreateTransaction(TransactionOptions properties)
        {
            OletxCommittableTransaction transaction          = null;
            ITransactionShim            transactionShim      = null;
            RealOletxTransaction        realOletxTransaction = null;
            Guid empty = Guid.Empty;
            OutcomeEnlistment target = null;

            new DistributedTransactionPermission(PermissionState.Unrestricted).Demand();
            TransactionManager.ValidateIsolationLevel(properties.IsolationLevel);
            if (IsolationLevel.Unspecified == properties.IsolationLevel)
            {
                properties.IsolationLevel = this.configuredTransactionOptions.IsolationLevel;
            }
            properties.Timeout = TransactionManager.ValidateTimeout(properties.Timeout);
            this.dtcTransactionManagerLock.AcquireReaderLock(-1);
            try
            {
                OletxTransactionIsolationLevel isolationLevel = ConvertIsolationLevel(properties.IsolationLevel);
                uint timeout = System.Transactions.Oletx.DtcTransactionManager.AdjustTimeout(properties.Timeout);
                target = new OutcomeEnlistment();
                IntPtr zero = IntPtr.Zero;
                RuntimeHelpers.PrepareConstrainedRegions();
                try
                {
                    zero = HandleTable.AllocHandle(target);
                    this.dtcTransactionManager.ProxyShimFactory.BeginTransaction(timeout, isolationLevel, zero, out empty, out transactionShim);
                }
                catch (COMException exception)
                {
                    ProxyException(exception);
                    throw;
                }
                finally
                {
                    if ((transactionShim == null) && (zero != IntPtr.Zero))
                    {
                        HandleTable.FreeHandle(zero);
                    }
                }
                realOletxTransaction = new RealOletxTransaction(this, transactionShim, target, empty, isolationLevel, true);
                transaction          = new OletxCommittableTransaction(realOletxTransaction);
                if (DiagnosticTrace.Information)
                {
                    TransactionCreatedTraceRecord.Trace(System.Transactions.SR.GetString("TraceSourceOletx"), transaction.TransactionTraceId);
                }
            }
            finally
            {
                this.dtcTransactionManagerLock.ReleaseReaderLock();
            }
            return(transaction);
        }
        internal OletxVolatileEnlistmentContainer AddDependentClone(bool delayCommit)
        {
            IVoterBallotShim      voterBallotShim = null;
            IPhase0EnlistmentShim shim2           = null;
            bool flag2 = false;
            bool flag  = false;
            OletxVolatileEnlistmentContainer       container3 = null;
            OletxPhase0VolatileEnlistmentContainer container  = null;
            OletxPhase1VolatileEnlistmentContainer target     = null;
            bool   flag3 = false;
            bool   flag5 = false;
            IntPtr zero  = IntPtr.Zero;

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                lock (this)
                {
                    if (delayCommit)
                    {
                        if (this.phase0EnlistVolatilementContainerList == null)
                        {
                            this.phase0EnlistVolatilementContainerList = new ArrayList(1);
                        }
                        if (this.phase0EnlistVolatilementContainerList.Count == 0)
                        {
                            container = new OletxPhase0VolatileEnlistmentContainer(this);
                            flag      = true;
                        }
                        else
                        {
                            container = this.phase0EnlistVolatilementContainerList[this.phase0EnlistVolatilementContainerList.Count - 1] as OletxPhase0VolatileEnlistmentContainer;
                            if (container != null)
                            {
                                this.TakeContainerLock(container, ref flag5);
                            }
                            if (!container.NewEnlistmentsAllowed)
                            {
                                this.ReleaseContainerLock(container, ref flag5);
                                container = new OletxPhase0VolatileEnlistmentContainer(this);
                                flag      = true;
                            }
                            else
                            {
                                flag = false;
                            }
                        }
                        if (flag)
                        {
                            zero = HandleTable.AllocHandle(container);
                        }
                    }
                    else if (this.phase1EnlistVolatilementContainer == null)
                    {
                        target             = new OletxPhase1VolatileEnlistmentContainer(this);
                        flag2              = true;
                        target.voterHandle = HandleTable.AllocHandle(target);
                    }
                    else
                    {
                        flag2  = false;
                        target = this.phase1EnlistVolatilementContainer;
                    }
                    try
                    {
                        if (container != null)
                        {
                            this.TakeContainerLock(container, ref flag5);
                        }
                        if (flag)
                        {
                            this.transactionShim.Phase0Enlist(zero, out shim2);
                            container.Phase0EnlistmentShim = shim2;
                        }
                        if (flag2)
                        {
                            this.OletxTransactionManagerInstance.dtcTransactionManagerLock.AcquireReaderLock(-1);
                            try
                            {
                                this.transactionShim.CreateVoter(target.voterHandle, out voterBallotShim);
                                flag3 = true;
                            }
                            finally
                            {
                                this.OletxTransactionManagerInstance.dtcTransactionManagerLock.ReleaseReaderLock();
                            }
                            target.VoterBallotShim = voterBallotShim;
                        }
                        if (delayCommit)
                        {
                            if (flag)
                            {
                                this.phase0EnlistVolatilementContainerList.Add(container);
                            }
                            container.AddDependentClone();
                            return(container);
                        }
                        if (flag2)
                        {
                            this.phase1EnlistVolatilementContainer = target;
                        }
                        target.AddDependentClone();
                        return(target);
                    }
                    catch (COMException exception)
                    {
                        OletxTransactionManager.ProxyException(exception);
                        throw;
                    }
                    return(container3);
                }
            }
            finally
            {
                if (container != null)
                {
                    this.ReleaseContainerLock(container, ref flag5);
                }
                if ((zero != IntPtr.Zero) && (container.Phase0EnlistmentShim == null))
                {
                    HandleTable.FreeHandle(zero);
                }
                if ((!flag3 && (target != null)) && ((target.voterHandle != IntPtr.Zero) && flag2))
                {
                    HandleTable.FreeHandle(target.voterHandle);
                }
            }
            return(container3);
        }
        internal IPromotedEnlistment CommonEnlistVolatile(IEnlistmentNotificationInternal enlistmentNotification, EnlistmentOptions enlistmentOptions, OletxTransaction oletxTransaction)
        {
            OletxVolatileEnlistment enlistment = null;
            bool flag2 = false;
            bool flag  = false;
            OletxPhase0VolatileEnlistmentContainer target    = null;
            OletxPhase1VolatileEnlistmentContainer container = null;
            IntPtr                zero            = IntPtr.Zero;
            IVoterBallotShim      voterBallotShim = null;
            IPhase0EnlistmentShim shim            = null;
            bool flag3 = false;

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                lock (this)
                {
                    enlistment = new OletxVolatileEnlistment(enlistmentNotification, enlistmentOptions, oletxTransaction);
                    if ((enlistmentOptions & EnlistmentOptions.EnlistDuringPrepareRequired) != EnlistmentOptions.None)
                    {
                        if (this.phase0EnlistVolatilementContainerList == null)
                        {
                            this.phase0EnlistVolatilementContainerList = new ArrayList(1);
                        }
                        if (this.phase0EnlistVolatilementContainerList.Count == 0)
                        {
                            target = new OletxPhase0VolatileEnlistmentContainer(this);
                            flag   = true;
                        }
                        else
                        {
                            target = this.phase0EnlistVolatilementContainerList[this.phase0EnlistVolatilementContainerList.Count - 1] as OletxPhase0VolatileEnlistmentContainer;
                            if (!target.NewEnlistmentsAllowed)
                            {
                                target = new OletxPhase0VolatileEnlistmentContainer(this);
                                flag   = true;
                            }
                            else
                            {
                                flag = false;
                            }
                        }
                        if (flag)
                        {
                            zero = HandleTable.AllocHandle(target);
                        }
                    }
                    else if (this.phase1EnlistVolatilementContainer == null)
                    {
                        flag2     = true;
                        container = new OletxPhase1VolatileEnlistmentContainer(this)
                        {
                            voterHandle = HandleTable.AllocHandle(container)
                        };
                    }
                    else
                    {
                        flag2     = false;
                        container = this.phase1EnlistVolatilementContainer;
                    }
                    try
                    {
                        if (flag)
                        {
                            lock (target)
                            {
                                this.transactionShim.Phase0Enlist(zero, out shim);
                                target.Phase0EnlistmentShim = shim;
                            }
                        }
                        if (flag2)
                        {
                            this.transactionShim.CreateVoter(container.voterHandle, out voterBallotShim);
                            flag3 = true;
                            container.VoterBallotShim = voterBallotShim;
                        }
                        if ((enlistmentOptions & EnlistmentOptions.EnlistDuringPrepareRequired) != EnlistmentOptions.None)
                        {
                            target.AddEnlistment(enlistment);
                            if (flag)
                            {
                                this.phase0EnlistVolatilementContainerList.Add(target);
                            }
                            return(enlistment);
                        }
                        container.AddEnlistment(enlistment);
                        if (flag2)
                        {
                            this.phase1EnlistVolatilementContainer = container;
                        }
                        return(enlistment);
                    }
                    catch (COMException exception)
                    {
                        OletxTransactionManager.ProxyException(exception);
                        throw;
                    }
                    return(enlistment);
                }
            }
            finally
            {
                if ((zero != IntPtr.Zero) && (target.Phase0EnlistmentShim == null))
                {
                    HandleTable.FreeHandle(zero);
                }
                if ((!flag3 && (container != null)) && ((container.voterHandle != IntPtr.Zero) && flag2))
                {
                    HandleTable.FreeHandle(container.voterHandle);
                }
            }
            return(enlistment);
        }
Esempio n. 5
0
        internal OletxCommittableTransaction CreateTransaction(
            TransactionOptions properties
            )
        {
            OletxCommittableTransaction tx = null;
            RealOletxTransaction        realTransaction = null;
            ITransactionShim            transactionShim = null;
            Guid txIdentifier = Guid.Empty;
            OutcomeEnlistment outcomeEnlistment = null;

            // Demand the distributed transation permission to create one of
            // these.
            DistributedTransactionPermission txPerm =
                new DistributedTransactionPermission(PermissionState.Unrestricted);

            txPerm.Demand();

            TransactionManager.ValidateIsolationLevel(properties.IsolationLevel);

            // Never create a transaction with an IsolationLevel of Unspecified.
            if (IsolationLevel.Unspecified == properties.IsolationLevel)
            {
                properties.IsolationLevel = configuredTransactionOptions.IsolationLevel;
            }

            properties.Timeout = TransactionManager.ValidateTimeout(properties.Timeout);

            this.dtcTransactionManagerLock.AcquireReaderLock(-1);
            try
            {
                //
                OletxTransactionIsolationLevel oletxIsoLevel = OletxTransactionManager.ConvertIsolationLevel(properties.IsolationLevel);
                UInt32 oletxTimeout = DtcTransactionManager.AdjustTimeout(properties.Timeout);

                outcomeEnlistment = new OutcomeEnlistment();
                IntPtr outcomeEnlistmentHandle = IntPtr.Zero;
                RuntimeHelpers.PrepareConstrainedRegions();
                try
                {
                    outcomeEnlistmentHandle = HandleTable.AllocHandle(outcomeEnlistment);

                    dtcTransactionManager.ProxyShimFactory.BeginTransaction(
                        oletxTimeout,
                        oletxIsoLevel,
                        outcomeEnlistmentHandle,
                        out txIdentifier,
                        out transactionShim
                        );
                }
                catch (COMException ex)
                {
                    OletxTransactionManager.ProxyException(ex);
                    throw;
                }
                finally
                {
                    if (transactionShim == null && outcomeEnlistmentHandle != IntPtr.Zero)
                    {
                        HandleTable.FreeHandle(outcomeEnlistmentHandle);
                    }
                }

                realTransaction = new RealOletxTransaction(
                    this,
                    transactionShim,
                    outcomeEnlistment,
                    txIdentifier,
                    oletxIsoLevel,
                    true
                    );
                tx = new OletxCommittableTransaction(realTransaction);
                if (DiagnosticTrace.Information)
                {
                    TransactionCreatedTraceRecord.Trace(SR.GetString(SR.TraceSourceOletx),
                                                        tx.TransactionTraceId
                                                        );
                }
            }
            finally
            {
                this.dtcTransactionManagerLock.ReleaseReaderLock();
            }

            return(tx);
        }
        void Initialize()
        {
            if (this.initialized)
            {
                return;
            }

            OletxInternalResourceManager internalRM = this.oletxTm.internalResourceManager;
            IntPtr handle = IntPtr.Zero;
            IResourceManagerShim resourceManagerShim = null;
            bool nodeNameMatches = false;

            CoTaskMemHandle whereaboutsBuffer = null;

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                handle = HandleTable.AllocHandle(internalRM);

                this.proxyShimFactory.ConnectToProxy(
                    this.nodeName,
                    internalRM.Identifier,
                    handle,
                    out nodeNameMatches,
                    out this.whereaboutsSize,
                    out whereaboutsBuffer,
                    out resourceManagerShim
                    );

                // If the node name does not match, throw.
                if (!nodeNameMatches)
                {
                    throw new NotSupportedException(SR.GetString(SR.ProxyCannotSupportMultipleNodeNames));
                }

                // Make a managed copy of the whereabouts.
                if ((null != whereaboutsBuffer) && (0 != this.whereaboutsSize))
                {
                    this.whereabouts = new byte[this.whereaboutsSize];
                    Marshal.Copy(whereaboutsBuffer.DangerousGetHandle(), this.whereabouts, 0, Convert.ToInt32(this.whereaboutsSize));
                }

                // Give the IResourceManagerShim to the internalRM and tell it to call ReenlistComplete.
                internalRM.resourceManagerShim = resourceManagerShim;
                internalRM.CallReenlistComplete();


                this.initialized = true;
            }
            catch (COMException ex)
            {
                if (NativeMethods.XACT_E_NOTSUPPORTED == ex.ErrorCode)
                {
                    throw new NotSupportedException(SR.GetString(SR.CannotSupportNodeNameSpecification));
                }

                OletxTransactionManager.ProxyException(ex);

                // Unfortunately MSDTCPRX may return unknown error codes when attempting to connect to MSDTC
                // that error should be propagated back as a TransactionManagerCommunicationException.
                throw TransactionManagerCommunicationException.Create(
                          SR.GetString(SR.TraceSourceOletx),
                          SR.GetString(SR.TransactionManagerCommunicationException),
                          ex
                          );
            }
            finally
            {
                if (null != whereaboutsBuffer)
                {
                    whereaboutsBuffer.Close();
                }

                // If we weren't successful at initializing ourself, clear things out
                // for next time around.
                if (!this.initialized)
                {
                    if (handle != IntPtr.Zero && null == resourceManagerShim)
                    {
                        HandleTable.FreeHandle(handle);
                    }

                    if (null != this.whereabouts)
                    {
                        this.whereabouts     = null;
                        this.whereaboutsSize = 0;
                    }
                }
            }
        }
Esempio n. 7
0
        internal OletxEnlistment EnlistDurable(OletxTransaction oletxTransaction, bool canDoSinglePhase, IEnlistmentNotificationInternal enlistmentNotification, EnlistmentOptions enlistmentOptions)
        {
            IResourceManagerShim  resourceManagerShim = null;
            IPhase0EnlistmentShim shim2          = null;
            IEnlistmentShim       enlistmentShim = null;
            IntPtr          zero   = IntPtr.Zero;
            bool            flag3  = false;
            bool            flag2  = false;
            OletxEnlistment target = new OletxEnlistment(canDoSinglePhase, enlistmentNotification, oletxTransaction.RealTransaction.TxGuid, enlistmentOptions, this, oletxTransaction);
            bool            flag   = false;

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                if ((enlistmentOptions & EnlistmentOptions.EnlistDuringPrepareRequired) != EnlistmentOptions.None)
                {
                    RuntimeHelpers.PrepareConstrainedRegions();
                    try
                    {
                    }
                    finally
                    {
                        oletxTransaction.RealTransaction.IncrementUndecidedEnlistments();
                        flag2 = true;
                    }
                }
                lock (target)
                {
                    RuntimeHelpers.PrepareConstrainedRegions();
                    try
                    {
                        resourceManagerShim = this.ResourceManagerShim;
                        if (resourceManagerShim == null)
                        {
                            throw TransactionManagerCommunicationException.Create(System.Transactions.SR.GetString("TraceSourceOletx"), null);
                        }
                        if ((enlistmentOptions & EnlistmentOptions.EnlistDuringPrepareRequired) != EnlistmentOptions.None)
                        {
                            zero = HandleTable.AllocHandle(target);
                            RuntimeHelpers.PrepareConstrainedRegions();
                            try
                            {
                            }
                            finally
                            {
                                oletxTransaction.RealTransaction.TransactionShim.Phase0Enlist(zero, out shim2);
                                flag3 = true;
                            }
                            target.Phase0EnlistmentShim = shim2;
                        }
                        target.phase1Handle = HandleTable.AllocHandle(target);
                        resourceManagerShim.Enlist(oletxTransaction.RealTransaction.TransactionShim, target.phase1Handle, out enlistmentShim);
                        target.EnlistmentShim = enlistmentShim;
                    }
                    catch (COMException exception)
                    {
                        if (System.Transactions.Oletx.NativeMethods.XACT_E_TOOMANY_ENLISTMENTS == exception.ErrorCode)
                        {
                            throw TransactionException.Create(System.Transactions.SR.GetString("TraceSourceOletx"), System.Transactions.SR.GetString("OletxTooManyEnlistments"), exception);
                        }
                        OletxTransactionManager.ProxyException(exception);
                        throw;
                    }
                    finally
                    {
                        if (target.EnlistmentShim == null)
                        {
                            if ((zero != IntPtr.Zero) && !flag3)
                            {
                                HandleTable.FreeHandle(zero);
                            }
                            if (target.phase1Handle != IntPtr.Zero)
                            {
                                HandleTable.FreeHandle(target.phase1Handle);
                            }
                        }
                    }
                }
                flag = true;
            }
            finally
            {
                if ((!flag && ((enlistmentOptions & EnlistmentOptions.EnlistDuringPrepareRequired) != EnlistmentOptions.None)) && flag2)
                {
                    oletxTransaction.RealTransaction.DecrementUndecidedEnlistments();
                }
            }
            return(target);
        }