public static Transaction GetTransactionFromExportCookie(byte[] cookie)
        {
            if (null == cookie)
            {
                throw new ArgumentNullException(nameof(cookie));
            }

            if (cookie.Length < 32)
            {
                throw new ArgumentException(SR.InvalidArgument, nameof(cookie));
            }

            TransactionsEtwProvider etwLog = TransactionsEtwProvider.Log;

            if (etwLog.IsEnabled())
            {
                etwLog.MethodEnter(TraceSourceType.TraceSourceDistributed, "TransactionInterop.GetTransactionFromExportCookie");
            }

            var cookieCopy = new byte[cookie.Length];

            Buffer.BlockCopy(cookie, 0, cookieCopy, 0, cookie.Length);
            cookie = cookieCopy;

            // Extract the transaction guid from the propagation token to see if we already have a
            // transaction object for the transaction.
            byte[] guidByteArray = new byte[16];
            for (int i = 0; i < guidByteArray.Length; i++)
            {
                // In a cookie, the transaction guid is preceeded by a signature guid.
                guidByteArray[i] = cookie[i + 16];
            }
            Guid txId = new Guid(guidByteArray);

            // First check to see if there is a promoted LTM transaction with the same ID.  If there
            // is, just return that.
            Transaction transaction = TransactionManager.FindPromotedTransaction(txId);

            if (transaction != null)
            {
                if (etwLog.IsEnabled())
                {
                    etwLog.MethodExit(TraceSourceType.TraceSourceDistributed, "TransactionInterop.GetTransactionFromExportCookie");
                }

                return(transaction);
            }

            // Find or create the promoted transaction.
            DistributedTransaction dTx = DistributedTransactionManager.GetTransactionFromExportCookie(cookieCopy, txId);

            transaction = TransactionManager.FindOrCreatePromotedTransaction(txId, dTx);

            if (etwLog.IsEnabled())
            {
                etwLog.MethodExit(TraceSourceType.TraceSourceDistributed, "TransactionInterop.GetTransactionFromExportCookie");
            }

            return(transaction);
        }
        internal static DistributedTransaction ConvertToDistributedTransaction(Transaction transaction)
        {
            if (null == transaction)
            {
                throw new ArgumentNullException(nameof(transaction));
            }

            if (transaction.Disposed)
            {
                throw new ObjectDisposedException(nameof(Transaction));
            }

            if (transaction._complete)
            {
                throw TransactionException.CreateTransactionCompletedException(transaction.DistributedTxId);
            }

            DistributedTransaction distributedTx = transaction.Promote();

            if (distributedTx == null)
            {
                throw DistributedTransaction.NotSupported();
            }
            return(distributedTx);
        }
        public static byte[] GetTransmitterPropagationToken(Transaction transaction)
        {
            if (null == transaction)
            {
                throw new ArgumentNullException(nameof(transaction));
            }

            TransactionsEtwProvider etwLog = TransactionsEtwProvider.Log;

            if (etwLog.IsEnabled())
            {
                etwLog.MethodEnter(TraceSourceType.TraceSourceDistributed, "TransactionInterop.GetTransmitterPropagationToken");
            }

            DistributedTransaction dTx = ConvertToDistributedTransaction(transaction);

            byte[] token = dTx.GetTransmitterPropagationToken();

            if (etwLog.IsEnabled())
            {
                etwLog.MethodExit(TraceSourceType.TraceSourceDistributed, "TransactionInterop.GetTransmitterPropagationToken");
            }

            return(token);
        }
        public static byte[] GetExportCookie(Transaction transaction, byte[] whereabouts)
        {
            if (null == transaction)
            {
                throw new ArgumentNullException(nameof(transaction));
            }

            if (null == whereabouts)
            {
                throw new ArgumentNullException(nameof(whereabouts));
            }

            TransactionsEtwProvider etwLog = TransactionsEtwProvider.Log;

            if (etwLog.IsEnabled())
            {
                etwLog.MethodEnter(TraceSourceType.TraceSourceDistributed, "TransactionInterop.GetExportCookie");
            }

            // Copy the whereabouts so that it cannot be modified later.
            var whereaboutsCopy = new byte[whereabouts.Length];

            Buffer.BlockCopy(whereabouts, 0, whereaboutsCopy, 0, whereabouts.Length);

            DistributedTransaction dTx = ConvertToDistributedTransaction(transaction);

            byte[] cookie = dTx.GetExportCookie(whereaboutsCopy);

            if (etwLog.IsEnabled())
            {
                etwLog.MethodExit(TraceSourceType.TraceSourceDistributed, "TransactionInterop.GetExportCookie");
            }

            return(cookie);
        }
Пример #5
0
        public static byte[] GetExportCookie(Transaction transaction, byte[] whereabouts)
        {
            if (null == transaction)
            {
                throw new ArgumentNullException(nameof(transaction));
            }

            if (null == whereabouts)
            {
                throw new ArgumentNullException(nameof(whereabouts));
            }

            if (DiagnosticTrace.Verbose)
            {
                MethodEnteredTraceRecord.Trace(SR.TraceSourceDistributed, "TransactionInterop.GetExportCookie");
            }

            // Copy the whereabouts so that it cannot be modified later.
            var whereaboutsCopy = new byte[whereabouts.Length];

            Buffer.BlockCopy(whereabouts, 0, whereaboutsCopy, 0, whereabouts.Length);

            DistributedTransaction dTx = ConvertToDistributedTransaction(transaction);

            byte[] cookie = dTx.GetExportCookie(whereaboutsCopy);

            if (DiagnosticTrace.Verbose)
            {
                MethodExitedTraceRecord.Trace(SR.TraceSourceDistributed, "TransactionInterop.GetExportCookie");
            }

            return(cookie);
        }
Пример #6
0
        static void Work(string rpcUrl)
        {
            //使用分布式事务
            using (DistributedTransaction distributedTransaction = new DistributedTransaction(rpcUrl))
            {
                var list = new List <ParticipantInfo>();

                var pi1 = new ParticipantInfo()
                {
                    Participant = _rpcServiceProxy.OrderService,
                    Method      = "AddOrderTry",
                    Args        = new object[] { "aaa", 10 }
                };


                var pi2 = new ParticipantInfo()
                {
                    Participant = _rpcServiceProxy.InventoryService,
                    Method      = "ReduceInventoryTry",
                    Args        = new object[] { "bbb", 10 }
                };

                list.Add(pi1);

                list.Add(pi2);

                distributedTransaction.Regist(list);
            }
        }
        public void DistributedTransactionTest()
        {
            //失败事务
            BaseBusiness <Base_UnitTest> _bus1 = new BaseBusiness <Base_UnitTest>();
            BaseBusiness <Base_UnitTest> _bus2 = new BaseBusiness <Base_UnitTest>("BaseDb_Test");

            _bus1.DeleteAll();
            _bus2.DeleteAll();
            Base_UnitTest data1 = new Base_UnitTest
            {
                Id       = Guid.NewGuid().ToString(),
                UserId   = "1",
                UserName = Guid.NewGuid().ToString()
            };
            Base_UnitTest data2 = new Base_UnitTest
            {
                Id       = Guid.NewGuid().ToString(),
                UserId   = "1",
                UserName = Guid.NewGuid().ToString()
            };
            Base_UnitTest data3 = new Base_UnitTest
            {
                Id       = Guid.NewGuid().ToString(),
                UserId   = "2",
                UserName = Guid.NewGuid().ToString()
            };
            DistributedTransaction distributedTransaction = new DistributedTransaction(_bus1.Service, _bus2.Service);

            distributedTransaction.BeginTransaction();
            _bus1.ExecuteSql("insert into Base_UnitTest(Id) values('10') ");
            _bus1.Insert(data1);
            _bus1.Insert(data3);
            _bus2.Insert(data1);
            _bus2.Insert(data2);
            bool succcess = distributedTransaction.EndTransaction();
            int  count1   = _bus1.GetIQueryable().Count();
            int  count2   = _bus2.GetIQueryable().Count();

            Assert.AreEqual(succcess, false);
            Assert.AreEqual(count1, 0);
            Assert.AreEqual(count2, 0);

            //成功事务
            _bus1.DeleteAll();
            _bus2.DeleteAll();
            distributedTransaction = new DistributedTransaction(_bus1.Service, _bus2.Service);
            distributedTransaction.BeginTransaction();
            _bus1.ExecuteSql("insert into Base_UnitTest(Id) values('10') ");
            _bus1.Insert(data1);
            _bus1.Insert(data3);
            _bus2.Insert(data1);
            _bus2.Insert(data3);
            succcess = distributedTransaction.EndTransaction();
            count1   = _bus1.GetIQueryable().Count();
            count2   = _bus2.GetIQueryable().Count();
            Assert.AreEqual(succcess, true);
            Assert.AreEqual(count1, 3);
            Assert.AreEqual(count2, 2);
        }
        public static Transaction GetTransactionFromTransmitterPropagationToken(byte[] propagationToken)
        {
            if (null == propagationToken)
            {
                throw new ArgumentNullException(nameof(propagationToken));
            }

            if (propagationToken.Length < 24)
            {
                throw new ArgumentException(SR.InvalidArgument, nameof(propagationToken));
            }

            TransactionsEtwProvider etwLog = TransactionsEtwProvider.Log;

            if (etwLog.IsEnabled())
            {
                etwLog.MethodEnter(TraceSourceType.TraceSourceDistributed, "TransactionInterop.GetTransactionFromTransmitterPropagationToken");
            }

            // Extract the transaction guid from the propagation token to see if we already have a
            // transaction object for the transaction.
            byte[] guidByteArray = new byte[16];
            for (int i = 0; i < guidByteArray.Length; i++)
            {
                // In a propagation token, the transaction guid is preceeded by two version DWORDs.
                guidByteArray[i] = propagationToken[i + 8];
            }

            var txId = new Guid(guidByteArray);

            // First check to see if there is a promoted LTM transaction with the same ID.  If there is, just return that.
            Transaction tx = TransactionManager.FindPromotedTransaction(txId);

            if (null != tx)
            {
                if (etwLog.IsEnabled())
                {
                    etwLog.MethodExit(TraceSourceType.TraceSourceDistributed, "TransactionInterop.GetTransactionFromTransmitterPropagationToken");
                }

                return(tx);
            }

            DistributedTransaction dTx = GetDistributedTransactionFromTransmitterPropagationToken(propagationToken);

            // If a transaction is found then FindOrCreate will Dispose the distributed transaction created.
            Transaction returnValue = TransactionManager.FindOrCreatePromotedTransaction(txId, dTx);

            if (etwLog.IsEnabled())
            {
                etwLog.MethodExit(TraceSourceType.TraceSourceDistributed, "TransactionInterop.GetTransactionFromTransmitterPropagationToken");
            }
            return(returnValue);
        }
Пример #9
0
        // Construct an internal transaction
        internal InternalTransaction(Transaction outcomeSource, DistributedTransaction distributedTx)
        {
            _promotedTransaction = distributedTx;

            _absoluteTimeout = long.MaxValue;

            // Store the initial creater as it will be the source of outcome events
            _outcomeSource = outcomeSource;

            // Initialize the hash
            _transactionHash = TransactionManager.TransactionTable.Add(this);

            // Start the transaction off as active
            TransactionState.TransactionStateNonCommittablePromoted.EnterState(this);

            // Until otherwise noted this transaction uses normal promotion.
            _promoteState = TransactionState.TransactionStateNonCommittablePromoted;
        }
Пример #10
0
        internal static DistributedTransaction ConvertToDistributedTransaction(Transaction transaction)
        {
            ArgumentNullException.ThrowIfNull(transaction);

            ObjectDisposedException.ThrowIf(transaction.Disposed, transaction);

            if (transaction._complete)
            {
                throw TransactionException.CreateTransactionCompletedException(transaction.DistributedTxId);
            }

            DistributedTransaction?distributedTx = transaction.Promote();

            if (distributedTx == null)
            {
                throw DistributedTransaction.NotSupported();
            }
            return(distributedTx);
        }
Пример #11
0
        public static byte[] GetTransmitterPropagationToken(Transaction transaction)
        {
            ArgumentNullException.ThrowIfNull(transaction);

            TransactionsEtwProvider etwLog = TransactionsEtwProvider.Log;

            if (etwLog.IsEnabled())
            {
                etwLog.MethodEnter(TraceSourceType.TraceSourceDistributed, "TransactionInterop.GetTransmitterPropagationToken");
            }

            ConvertToDistributedTransaction(transaction);
            byte[] token = DistributedTransaction.GetTransmitterPropagationToken();

            if (etwLog.IsEnabled())
            {
                etwLog.MethodExit(TraceSourceType.TraceSourceDistributed, "TransactionInterop.GetTransmitterPropagationToken");
            }

            return(token);
        }
Пример #12
0
        public static IDtcTransaction GetDtcTransaction(Transaction transaction)
        {
            ArgumentNullException.ThrowIfNull(transaction);

            TransactionsEtwProvider etwLog = TransactionsEtwProvider.Log;

            if (etwLog.IsEnabled())
            {
                etwLog.MethodEnter(TraceSourceType.TraceSourceDistributed, "TransactionInterop.GetDtcTransaction");
            }

            ConvertToDistributedTransaction(transaction);
            IDtcTransaction transactionNative = DistributedTransaction.GetDtcTransaction();

            if (etwLog.IsEnabled())
            {
                etwLog.MethodExit(TraceSourceType.TraceSourceDistributed, "TransactionInterop.GetDtcTransaction");
            }

            return(transactionNative);
        }
Пример #13
0
        public static IDtcTransaction GetDtcTransaction(Transaction transaction)
        {
            if (null == transaction)
            {
                throw new ArgumentNullException(nameof(transaction));
            }

            if (DiagnosticTrace.Verbose)
            {
                MethodEnteredTraceRecord.Trace(SR.TraceSourceDistributed, "TransactionInterop.GetDtcTransaction");
            }

            DistributedTransaction dTx = ConvertToDistributedTransaction(transaction);
            IDtcTransaction        transactionNative = dTx.GetDtcTransaction();

            if (DiagnosticTrace.Verbose)
            {
                MethodExitedTraceRecord.Trace(SR.TraceSourceDistributed, "TransactionInterop.GetDtcTransaction");
            }

            return(transactionNative);
        }
Пример #14
0
        public static byte[] GetTransmitterPropagationToken(Transaction transaction)
        {
            if (null == transaction)
            {
                throw new ArgumentNullException(nameof(transaction));
            }

            if (DiagnosticTrace.Verbose)
            {
                MethodEnteredTraceRecord.Trace(SR.TraceSourceDistributed, "TransactionInterop.GetTransmitterPropagationToken");
            }

            DistributedTransaction dTx = ConvertToDistributedTransaction(transaction);

            byte[] token = dTx.GetTransmitterPropagationToken();

            if (DiagnosticTrace.Verbose)
            {
                MethodExitedTraceRecord.Trace(SR.TraceSourceDistributed, "TransactionInterop.GetTransmitterPropagationToken");
            }

            return(token);
        }
        public static IDtcTransaction GetDtcTransaction(Transaction transaction)
        {
            if (null == transaction)
            {
                throw new ArgumentNullException(nameof(transaction));
            }

            TransactionsEtwProvider etwLog = TransactionsEtwProvider.Log;

            if (etwLog.IsEnabled())
            {
                etwLog.MethodEnter(TraceSourceType.TraceSourceDistributed, "TransactionInterop.GetDtcTransaction");
            }

            DistributedTransaction dTx = ConvertToDistributedTransaction(transaction);
            IDtcTransaction        transactionNative = dTx.GetDtcTransaction();

            if (etwLog.IsEnabled())
            {
                etwLog.MethodExit(TraceSourceType.TraceSourceDistributed, "TransactionInterop.GetDtcTransaction");
            }

            return(transactionNative);
        }
Пример #16
0
        protected EntityBase GetSingle(string cmdText, CommandType cmdType, IDbDataParameter[] cmdParams, DistributedTransaction DT)
        {
            EntityBase     result     = null;
            IDataReader    reader     = DataAccessHelper.ExecuteReader(cmdText, cmdType, cmdParams, DT);
            SafeDataReader safeReader = new SafeDataReader(reader);

            if (reader.Read())
            {
                result = this.CreateAndBuildEntity(safeReader);
            }
            reader.Close();
            return(result);
        }
Пример #17
0
 public virtual EntityBase GetSingle(EntityBase anEntity, DistributedTransaction DT)
 {
     return(dao.GetSingle(anEntity, DT));
 }
Пример #18
0
        public virtual string GetSingleValue(string CommandName, CommandType cmdType, IDbDataParameter[] ParamList, DistributedTransaction DT)
        {
            string ReturnValue = "";

            ReturnValue = dao.GetSingleValue(CommandName, cmdType, ParamList, DT);
            return(ReturnValue);
        }
Пример #19
0
 public virtual EntityList GetAll(EntityBase anEntity, DistributedTransaction DT)
 {
     return(dao.GetAll(anEntity, DT));
 }
Пример #20
0
 /// <summary>
 /// Inserts a entity into a table. The DAO object knows which table and field the
 /// entity is for. The DAO Object is initialized in the subclass
 /// </summary>
 /// <param name="anEntity"></param>
 /// <returns></returns>
 public virtual int Insert(EntityBase anEntity, DistributedTransaction DT)
 {
     return(dao.Insert(anEntity, DT));
 }
Пример #21
0
        public DataTable ReaderToTable(string cmdText, CommandType cmdType, IDbDataParameter[] cmdParams, DistributedTransaction DisTran)
        {
            IDataReader reader;
            DataTable   DT = new DataTable();

            reader = dao.GetAllDataReader(cmdText, cmdType, cmdParams, DisTran);
            if (reader != null)
            {
                if (reader.FieldCount > 0)
                {
                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        DataColumn DC = new DataColumn();
                        DC.ColumnName = reader.GetName(i).ToString();
                        DC.DataType   = reader.GetFieldType(i);
                        DT.Columns.Add(DC);
                    }
                }
                while (reader.Read())
                {
                    DataRow DR = DT.NewRow();
                    for (int j = 0; j < reader.FieldCount; j++)
                    {
                        DR[j] = reader[j];
                    }
                    DT.Rows.Add(DR);
                }
                return(DT);
            }
            return(null);
        }
Пример #22
0
 /// <summary>
 /// Updates a entity in a table. The DAO object knows which table and field the
 /// entity is for. The DAO Object is initialized in the subclass
 /// </summary>
 /// <param name="anEntity"></param>
 public virtual int Update(EntityBase anEntity, DistributedTransaction DT)
 {
     return(dao.Update(anEntity, DT));
 }
Пример #23
0
 public IDataReader GetAllDataReader(string CommandName, CommandType cmdType, IDbDataParameter[] cmdParams, DistributedTransaction DT)
 {
     return(GetDataReader(CommandName, cmdType, cmdParams, DT));
 }
Пример #24
0
 public EntityList GetAll(string Procedure, IDbDataParameter[] cmdParams, DistributedTransaction DT)
 {
     return(GetList(Procedure, this.CommandTypeUsed, cmdParams, DT));
 }
Пример #25
0
 /// <summary>
 /// Performs the insertion of a new entity into the appropriate table. It uses the subclass's
 /// CreateInsertParameters method and InsertCommand property
 /// </summary>
 /// <param name="anEntity"></param>
 /// <returns></returns>
 ///
 public virtual EntityBase GetSingle(EntityBase anEntity, DistributedTransaction DT)
 {
     IDbDataParameter[] cmdParams = this.CreateSelectParameters(anEntity);
     return(GetSingle(this.SelectCommand, this.CommandTypeUsed, cmdParams, DT));
 }
Пример #26
0
 /// <summary>
 /// Performs the deletion of an existing entity (identified by an id) into the appropriate table.
 /// It uses the subclass's CreateDeleteParameters method and DeleteCommand property
 /// </summary>
 /// <param name="anEntity"></param>
 /// <returns></returns>
 public virtual int Delete(EntityBase anEntity, DistributedTransaction DT)
 {
     IDbDataParameter[] cmdParams = this.CreateDeleteParameters(anEntity);
     return(DataAccessHelper.ExecuteNonQueryTransaction(DeleteCommand, CommandTypeUsed, cmdParams, DT));
 }
Пример #27
0
        internal static Transaction FindOrCreatePromotedTransaction(Guid transactionIdentifier, DistributedTransaction dtx)
        {
            Transaction?tx = null;
            Hashtable   promotedTransactionTable = PromotedTransactionTable;

            lock (promotedTransactionTable)
            {
                WeakReference?weakRef = (WeakReference?)promotedTransactionTable[transactionIdentifier];
                if (null != weakRef)
                {
                    tx = weakRef.Target as Transaction;
                    if (null != tx)
                    {
                        // If we found a transaction then dispose it
                        dtx.Dispose();
                        return(tx.InternalClone());
                    }
                    else
                    {
                        // an old, moldy weak reference.  Let's get rid of it.
                        lock (promotedTransactionTable)
                        {
                            promotedTransactionTable.Remove(transactionIdentifier);
                        }
                    }
                }

                tx = new Transaction(dtx);

                // Since we are adding this reference to the table create an object that will clean that entry up.
                tx._internalTransaction._finalizedObject = new FinalizedObject(tx._internalTransaction, dtx.Identifier);

                weakRef = new WeakReference(tx, false);
                promotedTransactionTable[dtx.Identifier] = weakRef;
            }
            dtx.SavedLtmPromotedTransaction = tx;

            FireDistributedTransactionStarted(tx);

            return(tx);
        }
Пример #28
0
        public string GetSingleValue(string cmdText, CommandType cmdType, IDbDataParameter[] cmdParams, DistributedTransaction DT)
        {
            string         result     = "";
            IDataReader    reader     = DataAccessHelper.ExecuteReader(cmdText, cmdType, cmdParams, DT);
            SafeDataReader safeReader = new SafeDataReader(reader);

            if (reader.Read())
            {
                result = safeReader[0].ToString();
            }
            return(result);
        }
Пример #29
0
 internal Transaction(DistributedTransaction distributedTransaction)
 {
     _isoLevel            = distributedTransaction.IsolationLevel;
     _internalTransaction = new InternalTransaction(this, distributedTransaction);
     _cloneId             = Interlocked.Increment(ref _internalTransaction._cloneCount);
 }
Пример #30
0
        protected IDataReader GetDataReader(string cmdText, CommandType cmdType, IDbDataParameter[] cmdParams, DistributedTransaction DT)
        {
            IDataReader reader = DataAccessHelper.ExecuteReader(cmdText, cmdType, cmdParams, DT);
            IDataReader currentDataReader;

            currentDataReader = reader;
            //reader.Close();
            return(currentDataReader);
        }