简体   繁体   English

处置后确定交易状态

[英]Determining the status of Transaction after disposal

I am trying to determine if the transaction has been completed or not, and depending on the results, I want to do other stuff in another thread. 我试图确定事务是否已完成,并且根据结果,我想在另一个线程中执行其他操作。

Consider the below TransactionScope: 考虑下面的TransactionScope:

using (TransactionScope scope = new TransactionScope())
{
    // Do stuff

    Scope.Complete();
}

Now, in another class, in a separate thread, I am going through a list of objects that have similar transactions: 现在,在另一个类的单独线程中,我将遍历具有类似事务的对象的列表:

private static void ProcessActions()
{
    while(true)
    {
        action = pendingActions[0];
        if (action.CurrentTransaction == null || 
            action.CurrentTransaction.TransactionInformation.Status == TransactionStatus.Committed)
        {
            // Proceed to do things!!
            remove = true;
        }
        else if (action.CurrentTransaction.TransactionInformation.Status == TransactionStatus.Aborted)
        {
            // Transaction has aborted. Remove this action from the list
            remove = true;
        }

        if (remove)
        {
            lock (pendingActions)
            {
                pendingActions.Remove(action);
                eventCount = pendingActions.Count;
            }
        }
    }
}

I set the CurrentTransaction for an action when I am creating a new action in the constructor: 在构造函数中创建新动作时,我为动作设置了CurrentTransaction:

public Action()
{
    CurrentTransaction = System.Transactions.Transaction.Current;
}

The problem is that by the time the other thread is processing the actions, action.CurrentTransaction is disposed, throwing System.ObjectDisposedException. 问题在于,当另一个线程处理这些动作时,action.CurrentTransaction被处置,并引发System.ObjectDisposedException。

How can I keep track of the status for each transaction in an Action before it gets disposed? 在处置之前,我如何跟踪每个交易的状态?

I believe I have found a solution by utilizing Transaction.TransactionCompleted Event . 我相信我已经利用Transaction.TransactionCompleted Event找到了解决方案。

I used the below code to assign a delegate to the TransactionCompleted event: 我使用以下代码将委托分配给TransactionCompleted事件:

System.Transactions.Transaction.Current.TransactionCompleted += new TransactionCompletedEventHandler(Mother.Current_TransactionCompleted);

In that method, I was able to iterate through my actions and determine which one has the corresponding origin Transaction. 通过这种方法,我可以遍历我的操作并确定哪个操作具有相应的原始事务。 Like this: 像这样:

public static void Current_TransactionCompleted(object sender, TransactionEventArgs e)
{
    var originatingTransaction = sender as System.Transactions.Transaction;

    lock (pendingActions)
    {
        for (int i = pendingActions.Count - 1; i >= 0; i--)
        {
            var action = pendingActions[i];

            if (originatingTransaction.Equals(action.CurrentTransaction))
            {
                var transactionStatus = e.Transaction.TransactionInformation.Status;
                if (transactionStatus == TransactionStatus.Committed)
                {
                    // Later in the code, I will do stuff if CurrentTransaction is null
                    action.CurrentTransaction = null;
                }
                else if (transactionStatus == TransactionStatus.Aborted)
                {
                    // if It's aborted, I will remove this action
                    pendingActions.RemoveAt(i);
                }
                // I will skip processing any actions that still have a Transaction with the status of "Processing"
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM