This project has moved and is read-only. For the latest updates, please go here.

Using Effort and getting "The transaction has aborted" error

Jan 25, 2016 at 10:05 PM
Edited Jan 25, 2016 at 10:07 PM
Hi

I am trying to use Effort for integration testing and I am seeing a DbUpdateExcpetion thrown. When I get to the inner most exception, I see: "transaction is aborted. The code works fine when I use the local sql db. Here is the code and stack trace. The issue happens in the method InsertAndGetDbFeatureAsync(featureStoreContext, feature.Id); when it is called for the second time in the for loop.
    public async Task AddFeatureFileAsync(IngestionFeatureFile ingestionFeatureFile, IngestionBuildInfo buildInfo, IFeatureStoreDependencyManager dependencyManager)
    {
        await ExecutionHelper.ExecuteTransactionWithRetryAsync(async () =>
        {
            using (var featureStoreContext = (FeatureStoreContext)this.contextFactory.Create())
            {
                using (var dbContextTransaction = featureStoreContext.Database.BeginTransaction())
                {
                    try
                    {
                        var dbFeatureGroupInfos = new List<DbFeatureGroupInfo>();
                        foreach (var featureGroup in ingestionFeatureFile.IngestionFeatureGroups)
                        {

                            var dbFeatureInfos = new List<DbFeatureInfo>();
                            foreach (var feature in featureGroup.ChildFeatures)
                            {
                                DbFeature dbFeature = await InsertAndGetDbFeatureAsync(featureStoreContext, feature.Id);
                                DbFeatureInfo dbFeatureInfo = await InsertFeatureInfoIfNewAsync(featureStoreContext, feature, dbFeature);

                                dependencyManager.SaveDbFeatureInfo(feature.ClassName, dbFeatureInfo.Id);
                                dependencyManager.SaveDbFeatureInfo(feature.Id, dbFeatureInfo.Id);

                                dbFeatureInfos.Add(dbFeatureInfo);
                            }

                            DbFeatureGroupInfo dbFeatureGroupInfo = await InsertFeatureGroupInfoIfNewAsync(featureStoreContext, featureGroup, dbFeatureInfos);
                            dbFeatureGroupInfos.Add(dbFeatureGroupInfo);
                        }

                        DbFeatureFileInfo dbFeatureFileInfo = await InsertDbFeatureFileInfoIfNewAsync(featureStoreContext, ingestionFeatureFile, dbFeatureGroupInfos);
                        await InsertDbBuildInfoIfFileNewInBranchAsync(featureStoreContext, buildInfo, dbFeatureFileInfo);

                        dbContextTransaction.Commit();
                    }
                    catch (Exception)
                    {
                       dbContextTransaction.Rollback();
                        throw;
                    }
                }
            }
        });
    }

    private static async Task<DbFeature> InsertAndGetDbFeatureAsync(FeatureStoreContext featureStoreContext, int featureId)
    {
        DbFeature dbFeature = await featureStoreContext.DbFeatures.SingleOrDefaultAsync(f => (f.DbFeatureId == featureId));

        if (dbFeature == null)
        {
            dbFeature = new DbFeature
            {
                DbFeatureId = featureId
            };

            featureStoreContext.DbFeatures.Add(dbFeature);
            await featureStoreContext.SaveChangesAsync();
        }

        return dbFeature;
    }
Stack trace:
System.Data.Entity.Infrastructure.DbUpdateException occurred
HResult=-2146233087
Message=An error occurred while updating the entries. See the inner exception for details.
Source=mscorlib
StackTrace:
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Velocity.Internal.FeatureStoreIngestionManager.<InsertAndGetDbFeatureAsync>d__9.MoveNext() in e:\velocity6\VelocityServiceV1\Shared\FeatureStoreModel\FeatureStoreManager\FeatureStoreIngestionManager.cs:line 148
InnerException:
   HResult=-2146233087
   Message=An error occurred while updating the entries. See the inner exception for details.
   Source=EntityFramework
   StackTrace:
        at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.<UpdateAsync>d__0.MoveNext()
     --- End of stack trace from previous location where exception was thrown ---
        at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
        at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
        at System.Data.Entity.Core.Objects.ObjectContext.<ExecuteInTransactionAsync>d__3d`1.MoveNext()
     --- End of stack trace from previous location where exception was thrown ---
        at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
        at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
        at System.Data.Entity.Core.Objects.ObjectContext.<SaveChangesToStoreAsync>d__39.MoveNext()
     --- End of stack trace from previous location where exception was thrown ---
        at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
        at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
        at System.Data.Entity.Core.Objects.ObjectContext.<SaveChangesInternalAsync>d__31.MoveNext()
   InnerException: 
        HResult=-2146232828
        Message=Exception has been thrown by the target of an invocation.
        Source=mscorlib
        StackTrace:
             at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
             at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
             at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
             at Effort.Internal.Common.DatabaseReflectionHelper.InsertEntity(ITable table, Object entity, Transaction transaction)
             at Effort.Internal.CommandActions.InsertCommandAction.CreateAndInsertEntity(ITable table, IList`1 memberBindings, Transaction transaction)
             at Effort.Internal.CommandActions.InsertCommandAction.ExecuteNonQuery(ActionContext context)
             at Effort.Provider.EffortEntityCommand.ExecuteNonQuery()
             at System.Data.Common.DbCommand.ExecuteNonQueryAsync(CancellationToken cancellationToken)
          --- End of stack trace from previous location where exception was thrown ---
             at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
             at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
             at System.Data.Entity.Utilities.TaskExtensions.CultureAwaiter`1.GetResult()
             at System.Data.Entity.Core.Mapping.Update.Internal.DynamicUpdateCommand.<ExecuteAsync>d__0.MoveNext()
          --- End of stack trace from previous location where exception was thrown ---
             at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
             at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
             at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.<UpdateAsync>d__0.MoveNext()
        InnerException: 
             HResult=-2146233087
             Message=The transaction has aborted.
             Source=NMemory
             StackTrace:
                  at NMemory.Transactions.Transaction.EnsureTransaction(Transaction& transaction, IDatabase database)
                  at NMemory.Tables.Table`2.Insert(TEntity entity, Transaction transaction)
                  at Effort.Internal.Common.DatabaseReflectionHelper.WrapperMethods.InsertEntity[TEntity](ITable`1 table, TEntity entity, Transaction transaction)
             InnerException: