EntityFramework-Plus icon indicating copy to clipboard operation
EntityFramework-Plus copied to clipboard

Strange 'System.InvalidOperationException: Invalid operation. The connection is closed.' exceptions

Open schuettecarsten opened this issue 4 years ago • 1 comments

Description

Sometimes we get strange System.InvalidOperationException: Invalid operation. The connection is closed. and System.NullReferenceException: Object reference not set to an instance of an object. exceptions. Our code runs as Hangfire jobs. Most time, jobs work fine. But if we run into failure condition, we get the first exception on the first try, the second exception on the retry, then the first exception again on the next try and so on. This did not happened with earlier versions of EntityFramework.Plus libraries. This exception also occurs on the same statement, which is not the first SQL on the job.

The LINQ statement:

	var query = this.Query().Where(x => x.TenantID == tenantID);
	query = query.IncludeOptimized(x => x.GroupMemberships);
	query = query.IncludeOptimized(x => x.GroupMemberships.Select(y => y.User));
	query = query.IncludeOptimized(x => x.AssignedProduct);
	query = query.IncludeOptimized(x => x.AssignedProduct.Product);
	return query;

Exception

System.InvalidOperationException: Invalid operation. The connection is closed.
   at System.Data.SqlClient.SqlDataReader.TryHasMoreResults(Boolean& moreResults)
   at System.Data.SqlClient.SqlDataReader.TryNextResult(Boolean& more)
   at System.Data.SqlClient.SqlDataReader.NextResult()
   at Z.EntityFramework.Plus.QueryFutureBatch.ExecuteQueries()
   at Z.EntityFramework.Plus.QueryFutureEnumerable`1.GetEnumerator()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Z.EntityFramework.Plus.QueryIncludeOptimizedParentQueryable`1.CreateEnumerable()
   at System.Threading.Tasks.Task`1.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Z.EntityFramework.Plus.LazyAsyncEnumerator`1.<FirstMoveNextAsync>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Data.Entity.Infrastructure.IDbAsyncEnumerableExtensions.<ForEachAsync>d__2`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at ...
System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Data.SqlClient.SqlDataReader.GetInt64(Int32 i)
   at lambda_method(Closure , Shaper )
   at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[TEntity](Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet)
   at lambda_method(Closure , Shaper )
   at System.Data.Entity.Core.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)
   at System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.RowNestedResultEnumerator.MaterializeRow()
   at System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.RowNestedResultEnumerator.MoveNext()
   at System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.ObjectQueryNestedEnumerator.TryReadToNextElement()
   at System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.ObjectQueryNestedEnumerator.ReadElement()
   at System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.ObjectQueryNestedEnumerator.MoveNext()
   at Z.EntityFramework.Plus.QueryFutureEnumerable`1.SetResult(IEnumerator`1 enumerator)
   at Z.EntityFramework.Plus.QueryFutureEnumerable`1.SetResult(DbDataReader reader)
   at Z.EntityFramework.Plus.QueryFutureBatch.ExecuteQueries()
   at Z.EntityFramework.Plus.QueryFutureEnumerable`1.GetEnumerator()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Z.EntityFramework.Plus.QueryIncludeOptimizedParentQueryable`1.CreateEnumerable()
   at System.Threading.Tasks.Task`1.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Z.EntityFramework.Plus.LazyAsyncEnumerator`1.<FirstMoveNextAsync>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Data.Entity.Infrastructure.IDbAsyncEnumerableExtensions.<ForEachAsync>d__2`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at ...

Further technical details

  • EF version: 6.2 (.NET)
  • EF Plus version: 5.1.8
  • Database Provider: Sql-Server (2017)

schuettecarsten avatar Dec 21 '20 07:12 schuettecarsten

Hello @schuettecarsten ,

This did not happened with earlier versions of EntityFramework.Plus libraries

Can you let us know which version of the library didn't throw the error so we could compare change?

Best Regards,

Jon


Performance Libraries context.BulkInsert(list, options => options.BatchSize = 1000); Entity Framework ExtensionsEntity Framework ClassicBulk OperationsDapper Plus

Runtime Evaluation Eval.Execute("x + y", new {x = 1, y = 2}); // return 3 C# Eval FunctionSQL Eval Function

JonathanMagnan avatar Dec 21 '20 14:12 JonathanMagnan

We also have the same exception in "Z.EntityFramework.Plus.EF6" Version="6.14.2". Will try to update to latest version and see if this will be resolved. If this helps, we experienced multiple timeout exceptions before the InvalidOperationException occurs.

zabulus avatar Mar 15 '23 11:03 zabulus

Hello @zabulus ,

Let us know when you will have more information.

I don't think this issue is fixed even using the latest version as it might be related to the multiple timeout exception.

Best Regards,

Jon

JonathanMagnan avatar Mar 15 '23 14:03 JonathanMagnan

Hello @zabulus,

Since our last conversation, we haven't heard from you.

We will close this issue, if you provide more information we will re-open it.

Don’t hesitate to contact us, we are always here to assist you.

Looking forward to hearing from you,

Jon

JonathanMagnan avatar Mar 28 '23 12:03 JonathanMagnan