-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The connection was not closed. The connection's current state is open. #12914
Comments
This issue is lacking enough information for us to effectively reproduce. Please post a runnable project/solution or complete code listing that demonstrates the behavior you are seeing. |
Unfortunately, like many exceptions I run into when upgrading EF versions, it happens randomly, and I cannot reproduce it every time (but it happens several times). |
@JeanCollas To me, this looks like it's probably caused by multiple threads making use of the same connection concurrently, which is not supported. |
Based on my observation and analysis of this issue in my project. It's issue with Entity Framework. Here explanation: Issue will happens in those scenarios, If While execution of one of the SQL statement in some case any exception happened in middle of process or timeout, multiple execution of same methods (Get Error information from database or Logging Error details in DB) with single instance of Entity Framework DBContext. This statement based on my observation :
If you want reproduce your local or another environment, It's hard too. I'm working some POC. I will Share your later. Solution is,
|
@kosurucs there are some inaccurate or unclear statements in your comment. EF definitely does support asynchronous programming; however, it does not support concurrent usage of the same DbContext or connection (which is very different). This is exactly the same as Dapper and most other ORMs that I'm aware of. It's important to understand what these concepts mean in order to properly use EF and .NET in general. When an exception happens, depending on the exception type, the physical connection may get closed (e.g. networking issue) or not (e.g. database constraint violation). The pooled DbConnection will be closed in any case (so the physical connection is returned to the pool), which is how things were designed to work. If you have a specific, concrete case where you believe EF Core is not behaving as it should, please open an issue with a code sample and we can discuss it. |
@roji , Sorry if my understanding is wrong on this issue or my approaching is wrong on this issue. Could you please help on this issue, Currently we are using EF (6.0.0.0) in our project ... Our DB hosted in Azure Portal. We are Frequently observing this issue on peak hours of day. Currently entire application is depending on EF to interact with DB. The connection was not closed. The connection's current state is connecting.AD_STACK_TRACE:System.InvalidOperationException: at
System.Data.ProviderBase.DbConnectionClosedConnecting.Boolean TryOpenConnection(System.Data.Common.DbConnection,
System.Data.ProviderBase.DbConnectionFactory, System.Threading.Tasks.TaskCompletionSource`1[System.Data.ProviderBase.DbConnectionInternal],
System.Data.Common.DbConnectionOptions)() at System.Data.SqlClient.SqlConnection.Boolean
TryOpenInner(System.Threading.Tasks.TaskCompletionSource`1[System.Data.ProviderBase.DbConnectionInternal])() at System.Data.SqlClient.SqlConnection.Boolean
TryOpen(System.Threading.Tasks.TaskCompletionSource`1[System.Data.ProviderBase.DbConnectionInternal])() at System.Data.SqlClient.SqlConnection.Open() at
System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch() at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open() at
System.Data.Entity.Infrastructure.DbExecutionStrategy+<>c__DisplayClass1.<Execute>b__0() at System.Data.Entity.Infrastructure.DbExecutionStrategy.Execute() at
System.Data.Entity.Core.EntityClient.EntityConnection.Open() at System.Data.Entity.Core.Objects.ObjectContext.EnsureConnection() at
System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction() at System.Data.Entity.Core.Objects.ObjectContext+<>c__DisplayClass65`1.
<ExecuteStoreQueryReliably>b__63() at System.Data.Entity.Infrastructure.DbExecutionStrategy.Execute() at
System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQueryReliably() at System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQuery() at
System.Data.Entity.Internal.LazyEnumerator`1.MoveNext() at System.Linq.Enumerable.FirstOrDefault() at Telematics.BusinessServices.ErrorService.GetErrorCode() at
ApiServices.Common.ErrorHandler.ErrorHandlerAttribute.HandleBusinessException() at ApiServices.Common.ErrorHandler.ErrorHandlerAttribute.OnException() at
System.Web.Http.Filters.ExceptionFilterAttribute.OnExceptionAsync() at System.Web.Http.Filters.ExceptionFilterAttribute+<ExecuteExceptionFilterAsyncCore>d__0.MoveNext()
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start() at System.Web.Http.Filters.ExceptionFilterAttribute.ExecuteExceptionFilterAsyncCore()` |
@kosurucs from a brief glance it seems like you may be trying to use the same connection (and DbContext) concurrently from two different threads. Database connections simply aren't threadsafe, and therefore can not be used concurrently. However, there isn't enough detail to know for sure. If you can't find the source of the issue, please open a new issue in a new issue on https://github.com/aspnet/EntityFramework6 or ask a question on stackoverflow. Be sure to include a minimal, runnable code sample that demonstrates the issue. |
I Use Database.EnsureCreatedAsync() and Such Problem Occurred , Then Remove It ,And Problem Resolved , |
I am wondering if it is a normal behavior that this throws an exception:
That is the kind of exception I get recently, following updates to the latest version of .Net Core.
As well as
The text was updated successfully, but these errors were encountered: