Пример #1
0
        void OnBodyCompleted(NativeActivityContext context, ActivityInstance completedInstance)
        {
            TransactedReceiveData transactedReceiveData = context.Properties.Find(TransactedReceiveData.TransactedReceiveDataExecutionPropertyName) as TransactedReceiveData;

            Fx.Assert(transactedReceiveData != null, "TransactedReceiveScope.OnBodyComplete - transactedreceivedata is null");

            //Non Nested
            if (!this.isNested.Get(context))
            {
                Fx.Assert(transactedReceiveData.InitiatingTransaction != null, "TransactedReceiveScope.OnBodyComplete - Initiating transaction is null");
                System.Transactions.CommittableTransaction committableTransaction = transactedReceiveData.InitiatingTransaction as System.Transactions.CommittableTransaction;
                //If the initiating transaction was a committable transaction => this is a server side only transaction. Commit it here instead of letting the dispatcher deal with it
                //since we are Auto Complete = false and we want the completion of the TransactedReceiveScope to initiate the Commit.
                if (committableTransaction != null)
                {
                    committableTransaction.BeginCommit(TransactionCommitAsyncCallback, committableTransaction);
                }
                else
                {
                    //If the initiating transaction was a dependent transaction instead => this is a flowed in transaction, let's just complete the dependent clone
                    System.Transactions.DependentTransaction dependentTransaction = transactedReceiveData.InitiatingTransaction as System.Transactions.DependentTransaction;
                    Fx.Assert(dependentTransaction != null, "TransactedReceiveScope.OnBodyComplete - DependentClone was null");
                    dependentTransaction.Complete();
                }
            }
            else //Nested scenario - e.g TRS inside a TSA and in a flow case :- we still need to complete the dependent transaction
            {
                System.Transactions.DependentTransaction dependentTransaction = transactedReceiveData.InitiatingTransaction as System.Transactions.DependentTransaction;
                if (dependentTransaction != null)
                {
                    dependentTransaction.Complete();
                }
            }
        }
Пример #2
0
 public DbTransactionScope(System.Transactions.IsolationLevel isolationLevel, int timeoutInSeconds = 30)
 {
     _transaction = new System.Transactions.CommittableTransaction(
         new System.Transactions.TransactionOptions()
     {
         IsolationLevel = isolationLevel,
         Timeout        = new TimeSpan(0, 0, timeoutInSeconds)
     });
 }
        protected OperationResult <T> PerformDataBaseCall <T>(string sql, CommandType commandType, IEnumerable <KeyValuePair <string, object> > parameters, Func <SqlCommand, T> callback) =>
        Get.OperationResult(() => {
            try {
                if (_disposed)
                {
                    throw new System.ObjectDisposedException("This transaction scope has already been disposed.");
                }

                if (_connection == null)
                {
                    _commitableTransaction = new System.Transactions.CommittableTransaction();
                    _connection            = new SqlConnection(_connectionString.ToString());
                    _connection.Open();
                    _connection.EnlistTransaction(_commitableTransaction);
                }

                parameters  = parameters ?? new KeyValuePair <string, object> [0];
                var command = new SqlCommand {
                    Connection     = _connection,
                    CommandTimeout = Timeout,
                    CommandText    = sql,
                    CommandType    = commandType
                };
                command.Parameters.AddRange(
                    parameters.Select(p => {
                    var parameter = new SqlParameter(p.Key, p.Value ?? DBNull.Value);
                    if (p.Value is string)
                    {
                        parameter.DbType = DbType.AnsiString;
                    }
                    else if (p.Value is XDocument)
                    {
                        parameter.DbType = DbType.Xml;
                        parameter.Value  = new System.Data.SqlTypes.SqlXml(((XDocument)p.Value).CreateReader());
                    }

                    return(parameter);
                }).ToArray()
                    );

                return(callback(command));
            }
            catch {
                FinishOperation(() => _commitableTransaction?.Rollback());
                throw;
            }
        });
Пример #4
0
 static void TransactionCommitCallback(IAsyncResult result)
 {
     System.Transactions.CommittableTransaction committableTransaction = result.AsyncState as System.Transactions.CommittableTransaction;
     Fx.Assert(committableTransaction != null, "TransactedReceiveScope - In the static TransactionCommitCallback, the committable transaction was null");
     try
     {
         committableTransaction.EndCommit(result);
     }
     catch (System.Transactions.TransactionException ex)
     {
         //At this point, the activity has completed. Since the runtime is enlisted in the transaction, it knows that the transaction aborted.
         //The runtime will do the right thing based on the AbortInstanceOnTransactionFailure flag. We simply trace out that the call to EndCommit failed from this static callback
         if (TD.TransactedReceiveScopeEndCommitFailedIsEnabled())
         {
             TD.TransactedReceiveScopeEndCommitFailed(committableTransaction.TransactionInformation.LocalIdentifier, ex.Message);
         }
     }
 }
        /// <summary>
        ///     Initializes a new instance of the <see cref="EffortTransaction" /> class.
        /// </summary>
        /// <param name="connection">
        ///     The <see cref="EffortTransaction" /> object.
        /// </param>
        /// <param name="isolationLevel">
        ///     The isolation level.
        /// </param>
        /// <exception cref="System.InvalidOperationException">
        ///     Ambient transaction is already set.
        /// </exception>
        public EffortTransaction(
            EffortConnection connection,
            System.Data.IsolationLevel isolationLevel)
        {
            if (System.Transactions.Transaction.Current != null)
            {
                throw new InvalidOperationException("Ambient transaction is already set.");
            }

            this.connection     = connection;
            this.isolationLevel = isolationLevel;

            // Initialize new ambient transaction
            System.Transactions.TransactionOptions options =
                new System.Transactions.TransactionOptions();

            options.IsolationLevel = TranslateIsolationLevel(isolationLevel);
            options.Timeout        = new TimeSpan(0, 0, connection.ConnectionTimeout);

            this.systemTransaction = new System.Transactions.CommittableTransaction(options);

            this.transaction = NMemory.Transactions.Transaction.Create(this.systemTransaction);
        }
Пример #6
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="EffortTransaction" /> class.
        /// </summary>
        /// <param name="connection">
        ///     The <see cref="EffortTransaction" /> object.
        /// </param>
        /// <param name="isolationLevel">
        ///     The isolation level.
        /// </param>
        /// <exception cref="System.InvalidOperationException">
        ///     Ambient transaction is already set.
        /// </exception>
        public EffortTransaction(
            EffortConnection connection,
            System.Data.IsolationLevel isolationLevel)
        {
            if (System.Transactions.Transaction.Current != null)
            {
                throw new InvalidOperationException("Ambient transaction is already set.");
            }

            this.connection = connection;
            this.isolationLevel = isolationLevel;

            // Initialize new ambient transaction
            System.Transactions.TransactionOptions options =
                new System.Transactions.TransactionOptions();

            options.IsolationLevel = TranslateIsolationLevel(isolationLevel);
            options.Timeout = new TimeSpan(0, 0, connection.ConnectionTimeout);

            this.systemTransaction = new System.Transactions.CommittableTransaction(options);

            this.transaction = NMemory.Transactions.Transaction.Create(this.systemTransaction);
        }
 public TransactionContext(System.Transactions.CommittableTransaction transaction)
 {
     this.transaction = transaction;
     this.completed   = false;
 }
 public TransactionContext()
 {
     this.transaction = null;
     this.completed   = false;
 }
Пример #9
0
        private static System.Transactions.CommittableTransaction CreateDefaultTransaction()
        {
            System.Transactions.CommittableTransaction transaction = new System.Transactions.CommittableTransaction(
                new System.Transactions.TransactionOptions
                {
                    IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted,
                    Timeout = DefaultTransactionTimeout
                });

            return transaction;
        }
 public TransactionContext(System.Transactions.CommittableTransaction transaction)
 {
     this.transaction = transaction;
     this.completed = false;
 }
 public TransactionContext()
 {
     this.transaction = null;
     this.completed = false;
 }