如何在事务中止时获取错误详细信息
问题描述:
我正在使用System.Transanction
和TransanctionCompleted
事件来检测异常事务。如何在事务中止时获取错误详细信息
我该如何找出失败的原因?是一种检测错误细节的方法吗?
答
你可以在你的交易方法赶上System.Transactions.TransactionException
try
{
//Create the transaction scope
using (TransactionScope scope = new TransactionScope())
{
//Register for the transaction completed event for the current transaction
Transaction.Current.TransactionCompleted += new TransactionCompletedEventHandler(Current_TransactionCompleted);
// proces the transaction
}
}
catch (System.Transactions.TransactionAbortedException ex)
{
Console.WriteLine(ex);
}
catch (System.Transactions.TransactionException ex)
{
Console.WriteLine(ex);
}
catch
{
Console.WriteLine("Cannot complete transaction");
throw;
}
交易完成事件处理
static void Current_TransactionCompleted(object sender, TransactionEventArgs e)
{
Console.WriteLine("A transaction has completed:");
Console.WriteLine("ID: {0}", e.Transaction.TransactionInformation.LocalIdentifier);
Console.WriteLine("Distributed ID: {0}", e.Transaction.TransactionInformation.DistributedIdentifier);
Console.WriteLine("Status: {0}", e.Transaction.TransactionInformation.Status);
Console.WriteLine("IsolationLevel: {0}", e.Transaction.IsolationLevel);
}
谢谢,我的意思是让交易完成事件中的错误信息,因为我只能访问到交易但不能由我的代码来决定是否提交/中止交易。再次感谢。 –
你必须在TransactionScope调用方法上做到这一点。 – Damith