如何查找交易状态

15
我正在使用“TransactionScope”,需要在C#代码中执行一些DML操作,这部分我已经成功完成。但是我需要知道事务状态,也就是它是否已经成功完成? 基于事务的状态,如果事务已经完成,我需要将其重定向到另一个页面;否则,如果事务未能成功完成,我需要在页面上显示错误信息。 在以下代码行之后,我希望进行重定向:
scope.Complete();
scope.Dispose();
4个回答

11

如果你访问MSDN页面上的TransactionScope,你会发现这个例子:

try
{
    // Create the TransactionScope to execute the commands, guaranteeing
    // that both commands can commit or roll back as a single unit of work.
    using (TransactionScope scope = new TransactionScope())
    {
        using (SqlConnection connection1 = new SqlConnection(connectString1))
        {
            // Opening the connection automatically enlists it in the 
            // TransactionScope as a lightweight transaction.
            connection1.Open();

            // Create the SqlCommand object and execute the first command.
            SqlCommand command1 = new SqlCommand(commandText1, connection1);
            returnValue = command1.ExecuteNonQuery();
            writer.WriteLine("Rows to be affected by command1: {0}", returnValue);

            // If you get here, this means that command1 succeeded. By nesting
            // the using block for connection2 inside that of connection1, you
            // conserve server and network resources as connection2 is opened
            // only when there is a chance that the transaction can commit.   
            using (SqlConnection connection2 = new SqlConnection(connectString2))
            {
                // The transaction is escalated to a full distributed
                // transaction when connection2 is opened.
                connection2.Open();

                // Execute the second command in the second database.
                returnValue = 0;
                SqlCommand command2 = new SqlCommand(commandText2, connection2);
                returnValue = command2.ExecuteNonQuery();
                writer.WriteLine("Rows to be affected by command2: {0}", returnValue);
            }
        }

        // The Complete method commits the transaction. If an exception has been thrown,
        // Complete is not  called and the transaction is rolled back.
        scope.Complete();

    }

}
catch (TransactionAbortedException ex)
{
    writer.WriteLine("TransactionAbortedException Message: {0}", ex.Message);
}
catch (ApplicationException ex)
{
    writer.WriteLine("ApplicationException Message: {0}", ex.Message);
}

这条评论中包含的最有价值的信息是:

Complete方法会提交事务。如果出现异常,就不会调用Complete方法并且事务会回滚。

所以,如果没有抛出异常,你可以继续进行操作。把重定向放在scope.Complete()之后。如果抛出了异常,事务将失败并自动回滚。你可以在调用Complete()并进行重定向之前,通过Transaction.Current.TransactionInformation.Status检查事务状态(正如其他人已经发布的)。

if (Transaction.Current.TransactionInformation.Status == TransactionStatus.Committed) 
{
    // do redirect
}

4

2
我发现最有效/正确的捕获方法如下:
在事务范围的using语句内,在调用scope/Complete()之前。
//Register for the transaction completed event for the current transaction
Transaction.Current.TransactionCompleted += new TransactionCompletedEventHandler(Current_TransactionCompleted);

然后创建以下事件处理程序函数:
/// <summary>
/// Handles the TransactionCompleted event of the Current control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Transactions.TransactionEventArgs"/> instance containing the event data.</param>
static void Current_TransactionCompleted(object sender, TransactionEventArgs e)
{
    if (e.Transaction.TransactionInformation.Status == TransactionStatus.Committed)
    {
        /// Yay it's committed code goes here!
    }
}

引用自MSDN

“您可以注册此事件,而不是使用易失性登记来获取事务的结果信息。传递给TransactionCompletedEventHandler委托的参数是一个Transaction实例。然后,您可以查询特定实例的TransactionInformation属性以获取TransactionInformation实例,其Status属性包含具有已提交或已中止值的事务的状态。”


2
如何呢:
TransactionStatus status = Transaction.Current.TransactionInformation.Status;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接