Как получить подробную информацию об ошибке, когда транзакция прервана - PullRequest
2 голосов
/ 03 ноября 2011

Я использую System.Transanction и событие TransanctionCompleted для обнаружения прерванных транзакций.

Как мне узнать, почему это не удалось? Есть ли способ обнаружить детали ошибки?

1 Ответ

1 голос
/ 03 ноября 2011

Вы можете перехватить 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);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...