【问题标题】:Determining the status of Transaction after disposal处置后确定交易状态
【发布时间】:2017-03-09 17:19:54
【问题描述】:

我正在尝试确定事务是否已完成,根据结果,我想在另一个线程中做其他事情。

考虑下面的 TransactionScope:

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

    Scope.Complete();
}

现在,在另一个类中,在一个单独的线程中,我正在查看具有相似事务的对象列表:

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

当我在构造函数中创建一个新动作时,我为一个动作设置了 CurrentTransaction:

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

问题是,当其他线程处理动作时,action.CurrentTransaction 被释放,抛出 System.ObjectDisposedException。

如何在 Action 中的每笔交易被处置之前跟踪其状态?

【问题讨论】:

  • 你想达到什么目的?
  • @VMAtm 我只是想检查每个操作的事务是否仍在处理、中止或提交,然后再继续执行其他操作。我想我已经找到了解决办法。在下面检查我的答案。

标签: c# multithreading transactions transactionscope


【解决方案1】:

我相信我已经通过使用Transaction.TransactionCompleted Event 找到了解决方案。

我使用以下代码将委托分配给 TransactionCompleted 事件:

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

在该方法中,我能够遍历我的操作并确定哪个具有相应的源事务。像这样:

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"
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-04
    • 2014-10-21
    • 2018-09-21
    • 1970-01-01
    • 2019-02-14
    • 1970-01-01
    相关资源
    最近更新 更多