e79aa3c0ed
Former-commit-id: a2155e9bd80020e49e72e86c44da02a8ac0e57a4
68 lines
2.2 KiB
C#
68 lines
2.2 KiB
C#
//-----------------------------------------------------------------------------
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
//-----------------------------------------------------------------------------
|
|
namespace System.Runtime
|
|
{
|
|
using System;
|
|
using System.Transactions;
|
|
|
|
static class TransactionHelper
|
|
{
|
|
public static void ThrowIfTransactionAbortedOrInDoubt(Transaction transaction)
|
|
{
|
|
if (transaction == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (transaction.TransactionInformation.Status == TransactionStatus.Aborted || transaction.TransactionInformation.Status == TransactionStatus.InDoubt)
|
|
{
|
|
//This will throw TransactionAbortedException/TransactionInDoubtException with corresponding inner exception if any
|
|
using (TransactionScope scope = new TransactionScope(transaction))
|
|
{
|
|
//empty
|
|
}
|
|
}
|
|
}
|
|
|
|
// If the transaction has aborted then we switch over to a new transaction
|
|
// which we will immediately abort after setting Transaction.Current
|
|
public static TransactionScope CreateTransactionScope(Transaction transaction)
|
|
{
|
|
try
|
|
{
|
|
return transaction == null ? null : new TransactionScope(transaction);
|
|
}
|
|
catch (TransactionAbortedException)
|
|
{
|
|
CommittableTransaction tempTransaction = new CommittableTransaction();
|
|
try
|
|
{
|
|
return new TransactionScope(tempTransaction.Clone());
|
|
}
|
|
finally
|
|
{
|
|
tempTransaction.Rollback();
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void CompleteTransactionScope(ref TransactionScope scope)
|
|
{
|
|
TransactionScope localScope = scope;
|
|
if (localScope != null)
|
|
{
|
|
scope = null;
|
|
try
|
|
{
|
|
localScope.Complete();
|
|
}
|
|
finally
|
|
{
|
|
localScope.Dispose();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|