事务不回滚

发布于 2024-09-28 04:27:14 字数 5587 浏览 7 评论 0原文

我有一个转换似乎没有回滚并且出现了一个奇怪的错误。我使用的事务与代码连接到不同服务器上的 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()

我不确定我做错了什么以及导致此问题的原因。我似乎无法生成相同的异常,并且我生成的每个异常总是回滚。有人知道我怎么可能产生这个错误吗?

感谢大家的帮助!

I have a transation that doesn't seem to rollback and getting a strange error. I'm using a transaction that is connected to SQL Server 2008 on a different server and MSMQ on the same server as the code. Here is an example of the code:

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);
    }

When this happens everything before SendTransactionMsmqMsg doesn't get rolled back. It's very rare this occurs too. Here is some of the stack trace too:

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()

I'm not sure what I'm doing wrong and what is causing this issue. I can't seem to generate this same exception and every exception I generate always rollback back. Anyone know how I may be able to generate this error at all?

thanks for everyones help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凡尘雨 2024-10-05 04:27:14

DTC 是否在事务内完成的工作所涉及的所有服务器上运行?所有进程都可以正确访问它吗?

这篇文章有与您收到的相同的错误消息:
TransactionScope 内的 Membership.GetUser() 抛出 TransactionPromotionException

更新

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

Is the DTC running on all servers involved in the work being done inside the transaction? Do all processes have proper access to it?

This post has the same error message you are getting:
Membership.GetUser() within TransactionScope throws TransactionPromotionException

Update

Where is the message queue object being instantiated? I would try instantiating it inside of the transaction scope.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文