Member 3749400 Ответов: 1

Try catch - транзакция, связанная с текущим соединением, завершена, но не удалена


I am getting Error : The transaction associated with the current connection has completed but has not been disposed. after Log.WriteEntry($"Error occurred while trying to create invoice for transaction {transaction.TransactionLogId} : {ex.Message}"); inside inner catch statement when i run the code.. Can someone please help?

public virtual GroupCreationResult CreateGroups(IEnumerable<TransactionDetail> transactions)
    {
        var transactionDetails = transactions as TransactionDetail[] ?? transactions.ToArray();
        var successes = new List<int>(transactionDetails.Length);
        var errors = new List<TransactionError>();

        foreach (var transaction in transactionDetails)
        {
            using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted, Timeout = new TimeSpan(0, 2, 0) }))
            {
                try
                {

                    foreach (var service in _invoiceCreationServices)
                    {
                        try
                        {
                            service.CreateInvoice(transaction);
                        }
                        catch (Exception ex)
                        {
                            Log.WriteEntry($"Error occurred while trying to create invoice for transaction  {transaction.TransactionLogId} : {ex.Message}");
                            if (!(ex.ToString().Contains("Violation of PRIMARY KEY constraint 'PK_APInvGrp'.") || ex.ToString().Contains("Violation of PRIMARY KEY constraint .")))
                            {
                                Log.WriteEntry($"error occured while adding the transaction {transaction.TransactionLogId} - {ex.ToString()}");
                                errors.Add(new TransactionError(transaction.TransactionLogId, ex.ToString()));
                                scope.Complete();
                                break;
                            }                               
                        }
                    }
                    Log.WriteEntry($"successfully added the transaction {transaction.TransactionLogId}");
                    successes.Add(transaction.TransactionLogId);
                    scope.Complete();
                }
                catch (Exception exception)
                {
                    Log.WriteEntry($"error1 occured while adding the transaction {transaction.TransactionLogId} - {exception.ToString()}");
                    //errors.Add(new TransactionError(transaction.TransactionLogId, exception.ToString()));
                }
            }
        }

        return BuildGroupCreationResult(successes, errors);
    }


Что я уже пробовал:

пытался изменить его, но все еще не работает

1 Ответов

Рейтинг:
0

Sandeep Mewara

вероятно, время вашей транзакции истекло. Создайте область транзакции с большим временным интервалом в качестве таймаута, и она пройдет.

Похоже, люди сталкивались с этим, когда время транзакции истекало.

Вы можете увеличить время ожидания для вашей транзакции, например:
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, new System.TimeSpan(0, 10, 0)))
{
  // 10 min timeout 
  // working code here
}