What is the reason of “Transaction context in use by another session”

前端 未结 6 576
孤独总比滥情好
孤独总比滥情好 2020-12-10 02:42

I\'m looking for a description of the root of this error: \"Transaction context in use by another session\".

I get it sometimes in one of my unittests so I can\'t pr

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-10 03:39

    You must create a DependentTransaction for each thread an then inside the thread create & open the db connection inside a TransacctionScope using the dependentTransaction in the ctor.

                //client code / main thread
                using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew, timeout))
                {
                    Transaction currentTransaction = Transaction.Current;
                    currentTransaction.TransactionCompleted += OnCompleted;
                    DependentTransaction dependentTransaction;
                    int nWorkers = Config.Instance.NumDBComponentWorkers;
                    for (int i = 0; i < nWorkers; i++)
                    {
                        dependentTransaction = currentTransaction.DependentClone(DependentCloneOption.BlockCommitUntilComplete);
                        this.startWorker(dependentTransaction);
                    }
                    do
                    {
                        //loop + wait
                        Thread.Sleep(150);
                    } while (this.status == DBComponentStatus.Running);
                    //No errors-commit transaction
                    if (this.status == DBComponentStatus.Finished && this.onCanCommit())
                    {
                        scope.Complete();
                    }
                }
    
        //workers
        protected override void startWorker(DependentTransaction dependentTransaction)
        {
            Thread thread = new Thread(workerMethod);
            thread.Start(dependentTransaction);
        }
    
        protected override void workerMethod(object transaction)
        {
            int executedStatements = 0;
            DependentTransaction dependentTransaction;
            dependentTransaction = transaction as DependentTransaction;
            System.Diagnostics.Debug.Assert(dependentTransaction != null); //testing
            try
            {
                //Transaction.Current = dependentTransaction;
                using (TransactionScope scope = new TransactionScope(dependentTransaction))
                {
                    using (SqlConnection conn = new SqlConnection(this.GetConnectionString(this.parameters)))
                    {
                        /* Perform transactional work here */
                        conn.Open();
                        string statement = string.Empty;
                        using (SqlCommand cmd = conn.CreateCommand())
                        {
    
                        }
                    }
                    //No errors-commit transaction
                    if (this.status == DBComponentStatus.Finished)
                    {
                        scope.Complete();
                    }
                }
            }
            catch (Exception e)
            {
                this.status = DBComponentStatus.Aborted;
            }
            finally
            {
                dependentTransaction.Complete();
                dependentTransaction.Dispose();
            }
        }
    

提交回复
热议问题