交易没有回滚

时间:2010-10-18 20:59:34

标签: c# sql-server transactions msmq transactionscope

我有一个似乎没有回滚并发生奇怪错误的转换。我正在使用在不同服务器上连接到SQL Server 2008的事务,并在与代码相同的服务器上使用MSMQ。以下是代码示例:

void MyMethod()
    {
       try
       {
           using (var outterTrans = new TransactionScope())
           {
               try
               {
                  InsertA();
                  SendTransactionMsmqMsg(new BlahObject());
               }
               catch (Exception e)
               {
                   //Exception is getting written here.
                   using (new TransactionScope(TransactionScopeOption.Suppress))                      
                      HandleException(e);

                    throw;
                }

                InsertB();
                outterTrans.Complete();
           }
        }
        catch(Exception e)
        {
            //Exception is getting written here too.
            HandleException(e);
        }
    }

    public void InsertA()
    {
       //Inserts data.
    }


    public void InsertB()
    {
       //Inserts data.
    }

    void SendTransactionMsmqMsg(object obj)
    {
         //When calling Msmq.Send I get 'The PROMOTE TRANSACTION request failed because there is no local transaction active.'
         Msmq.Send(CreateMessage(obj), MessageQueueTransactionType.Automatic);
    }

当发生这种情况时,SendTransactionMsmqMsg之前的所有内容都不会回滚。这种情况也很罕见。这也是一些堆栈跟踪:

Type : System.Transactions.TransactionAbortedException, System.Transactions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
    Message : The transaction has aborted.
    Source : System.Transactions
    Help link : 
    Data : System.Collections.ListDictionaryInternal
    TargetSite : Void CheckForFinishedTransaction(System.Transactions.InternalTransaction)
    Stack Trace :    at System.Transactions.TransactionStateAborted.CheckForFinishedTransaction(InternalTransaction tx)
       at System.Transactions.EnlistableStates.Promote(InternalTransaction tx)
       at System.Transactions.Transaction.Promote()
       at System.Transactions.TransactionInterop.ConvertToOletxTransaction(Transaction transaction)
       at System.Transactions.TransactionInterop.GetDtcTransaction(Transaction transaction)
       at System.Messaging.MessageQueue.StaleSafeSendMessage(MQPROPS properties, IntPtr transaction)
       at System.Messaging.MessageQueue.SendInternal(Object obj, MessageQueueTransaction internalTransaction, MessageQueueTransactionType transactionType)
       at System.Messaging.MessageQueue.Send(Object obj, MessageQueueTransactionType transactionType)
       ********Functions from my code here********

        Inner Exception
        ---------------
        Type : System.Transactions.TransactionPromotionException, System.Transactions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
        Message : Failure while attempting to promote transaction.
        Source : System.Data
        Help link : 
        Data : System.Collections.ListDictionaryInternal
        TargetSite : Byte[] Promote()
        Stack Trace :    at System.Data.SqlClient.SqlDelegatedTransaction.Promote()
           at System.Transactions.TransactionStatePSPEOperation.PSPEPromote(InternalTransaction tx)
           at System.Transactions.TransactionStateDelegatedBase.EnterState(InternalTransaction tx)

            Inner Exception
            ---------------
            Type : System.Data.SqlClient.SqlException, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
            Message : The PROMOTE TRANSACTION request failed because there is no local transaction active.
            Source : .Net SqlClient Data Provider
            Help link : 
            Errors : System.Data.SqlClient.SqlErrorCollection
            Class : 16
            LineNumber : 1
            Number : 3965
            Procedure : 
            Server : <machine>
            State : 1
            ErrorCode : -2146232060
            Data : System.Collections.ListDictionaryInternal
            TargetSite : Void OnError(System.Data.SqlClient.SqlException, Boolean)
            Stack Trace :    at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
               at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
               at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
               at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
               at System.Data.SqlClient.TdsParser.TdsExecuteTransactionManagerRequest(Byte[] buffer, TransactionManagerRequestType request, String transactionName, TransactionManagerIsolationLevel isoLevel, Int32 timeout, SqlInternalTransaction transaction, TdsParserStateObject stateObj, Boolean isDelegateControlRequest)
               at System.Data.SqlClient.SqlInternalConnectionTds.ExecuteTransactionYukon(TransactionRequest transactionRequest, String transactionName, IsolationLevel iso, SqlInternalTransaction internalTransaction, Boolean isDelegateControlRequest)
               at System.Data.SqlClient.SqlInternalConnectionTds.ExecuteTransaction(TransactionRequest transactionRequest, String name, IsolationLevel iso, SqlInternalTransaction internalTransaction, Boolean isDelegateControlRequest)
               at System.Data.SqlClient.SqlDelegatedTransaction.Promote()

我不确定我做错了什么以及导致这个问题的原因。我似乎无法生成同样的异常,并且我生成的每个异常都会回滚。任何人都知道我怎么能够产生这个错误?

感谢大家的帮助!

1 个答案:

答案 0 :(得分:1)

DTC是否在所有涉及工作的服务器上运行?所有进程都有适当的访问权限吗?

此帖子收到的错误消息相同: Membership.GetUser() within TransactionScope throws TransactionPromotionException

<强>更新

实例化消息队列对象在哪里?我会尝试在事务范围内实例化它。