3

My team and I have been developing this MVC application for about 3 years now, and all of the sudden we starting getting errors when communicating with the DB, and after some desperate search, and several users' discomfort, we were able to actually get it up and running by adding Pooling=False to the connection string for our EF DbContext.

I have been making some research and came across these links:

For my DbContext objects I have the Lifetime Scope set as InstancePerRequest() which, if I'm not wrong, means that once the request is served/finished then Autofac will call the Dispose() method on the DbContext and it should release the SqlConnection and ideally return it to the pool.

So my question is:

Should I do anything special to manually call DbContext.Dispose()?
Is there anything I'm missing how EF handles DB connections?

Edit

This is part of my logs:

"TypeName":"System.InvalidOperationException",
"Message":"Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.", "Source":"System.Data",
"Detail":"System.Data.Entity.Core.EntityException: The underlying provider failed on Open. --->
System.InvalidOperationException: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.

that's the error I was getting when attempting to connect to the DB

Luiso
  • 4,173
  • 2
  • 37
  • 60
  • 1
    What error(s) were you getting with pooling enabled? Also, do you know if anything changed in your SQL Server environment that might have caused the problem? – Jeff Siver Jul 11 '17 at 13:31
  • 1
    @JeffSiver I have just updated my question with the exception message I got. Hope that helps. Thank you – Luiso Jul 11 '17 at 14:09
  • Unless something external changed (e.g. way more users, SQL Server changes limiting connections, etc.), I would review the code changes that happened before the problem occurred. My guess is that something in the code changed to cause the connections to be held onto longer than previously. Also, have you made sure this isn't the problem - http://docs.autofac.org/en/latest/faq/per-request-scope.html#no-per-request-filter-dependencies-in-web-api. – Jeff Siver Jul 11 '17 at 14:23
  • There is quite a bit of info about how to deal with disposable per-request dependencies in the [docs](http://docs.autofac.org/en/latest/faq/per-request-scope.html). Specifically, you should take a look at the tests and write some for your application that verify `Dispose()` is indeed being called with your particular setup. At the very least, you should be able to determine this by setting a breakpoint. – NightOwl888 Jul 11 '17 at 14:23

0 Answers0