Async transaction Entity Framework 6.2

Viewed 317

Good morning!

I have long running async transcation with fails with some different errors.

THere is method with run the transaction

public T PerformInTransaction<T>(Func<ITransaction> beginTransaction, Func<ITransaction, T> func)
        {
            try
            {
                AzureDbConfiguration.SuspendExecutionStrategy = true;
                using (var transaction = beginTransaction())
                {
                    return func(transaction);
                }
            }
            finally
            {
                AzureDbConfiguration.SuspendExecutionStrategy = false;
            }
        }

The ITranscation interface is implemented as default of EF DbContextTransaction class as below:

public class Transaction : ITransaction
    {
        private readonly DbContextTransaction _transaction;

        public Transaction(DbContextTransaction transaction)
        {
            _transaction = transaction;
        }

        public Task CommitAsync()
        {
            this.Commit();

            return Task.CompletedTask;
        }

.... more methods
}

I have preety long operation running in transcation where inside I have loop with another async methods and each method might call SaveChanges(). Snippet basically looks like

await _unitOfWork.PerformInTransaction(async transaction =>
{ 
   foreach()
   {
      if(await Action())
        _unitOfWork.SaveChanges()
      else   
        transaction.Rollback()

   }

    transaction.Commit()  
})

The errors I have

System.InvalidOperationException: Invalid attempt to call IsDBNull when reader is closed. at System.Data.SqlClient.SqlDataReader.CheckHeaderIsReady(Int32 columnIndex, Boolean permitAsync, String methodName) at System.Data.SqlClient.SqlDataReader.IsDBNull(Int32 i) at lambda_method(Closure , Shaper ) at System.Data.Entity.Core.Common.Internal.Materialization.Coordinator1.ReadNextElement(Shaper shaper) at System.Data.Entity.Core.Common.Internal.Materialization.Shaper1.SimpleEnumerator.d__4.MoveNext()

or another

System.Data.Entity.Core.EntityException: The underlying provider failed on Commit. ---> System.ArgumentNullException: Value cannot be null. Parameter name: connection at System.Data.Entity.Utilities.Check.NotNull[T](T value, String parameterName) at System.Data.Entity.Infrastructure.Interception.DbTransactionInterceptionContext.WithConnection(DbConnection connection)

Do I do something wrong with async transcations? Is it related to some timouts ? Thanks in advance

0 Answers
Related