Click here to Skip to main content
15,888,098 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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?

C#
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);
    }


What I have tried:

tried to change it but still not working
Posted
Updated 31-Aug-20 20:02pm
v3

1 solution

your transaction probably timed out. Create the transaction scope with a large TimeSpan as the Timeout and it will get through.

Seems people have faced this when the transaction times out.

You can increase the timeout for your transaction, example:
C#
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, new System.TimeSpan(0, 10, 0)))
{
  // 10 min timeout 
  // working code here
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900