0

Only on production, I'm getting this error: "Execution of the command requires an open and available connection. The connection's current state is closed." What's weird is where its saying its happening. It's happening in a place that's never happened in all of our development, testing, or UAT sessions.... here is the underlying process.

We use the MVP pattern. This isn't as important, but here's essentially the flow:

  • Presenter instantiates an associated repository and call a method to query the appropriate data, and pass it to the model. The query is still live against the database at this point (not iterated through or had ToList called).
  • View binds this data to a drop down control. Here, when it iterates through to create the list of items, the error occurs.

We are binding around 8 drop downs at this point. Again, I've not experienced this at any other point - this isn't consistent enough either. Any ideas?

Thanks.

Brian Mains
  • 49,697
  • 35
  • 139
  • 249
  • 1
    Are you properly disposing your contexts by using `using` blocks or calling `Dispose()`? – Adam Robinson Sep 26 '11 at 16:25
  • Were you UATing against the production DB? If not, is the staging DB exactly like production? If not, you might have issues with production throttling connections, or timing them out. Or, you're trying to run multiple queries with the same connection. In that case, enable MARS. – Dustin Davis Sep 26 '11 at 16:34
  • Staging setup the same as PROD, but the environment is a little different. MARS is enabled. ObjectContext is retained in a single instance by storing it in HttpContext.Current.Items collection, stored for a single HTTP request. I can't accurately reproduce - I just accessed the same page several times, and it worked OK... – Brian Mains Sep 26 '11 at 16:41
  • Maybe connection pooling issue? – Dustin Davis Sep 26 '11 at 16:48
  • OK... is there a way to figure that out? – Brian Mains Sep 26 '11 at 16:58
  • @Brian: I given that you're sticking the context in the request store, I'm getting the impression that you aren't even explicitly disposing of it. Add code that always disposes of it at the end of the request and see if that addresses the issue. If it does, I'll post this as an answer. – Adam Robinson Sep 26 '11 at 17:04
  • @Adam what does it matter if he's disposing of it? This is a web app so it's going to be disposed when the request ends. – Dustin Davis Sep 26 '11 at 17:10
  • @Brain, you should try creating a new context when you need to instead of storing it in HttpContext. Have a look at http://stackoverflow.com/questions/3653009/entity-framework-and-connection-pooling – Dustin Davis Sep 26 '11 at 17:13
  • 1
    @DustinDavis: No, it will be *collected* at *some point*, just like in any other application. **Garbage collection is not the same as (or a replacement for) disposal of `IDisposable` objects.** – Adam Robinson Sep 26 '11 at 17:24
  • @Adam, yes I know that. I just realized that we're both right. If he isn't disposing of the context, the conenction isnt being closed properly and thus maxing out his connection pool. – Dustin Davis Sep 26 '11 at 17:33
  • @DustinDavis - creating a new OC doesn't play well when working with multiple entities (and is the result of "the object being modified is attached to a different context" error). – Brian Mains Sep 26 '11 at 18:41
  • @Brian: Are you disposing of the contexts...? – Adam Robinson Sep 26 '11 at 19:16
  • @Adam not explicitly no, I'm working to add that and see if that resolves the issue.... – Brian Mains Sep 27 '11 at 12:53

1 Answers1

0

I suspect your connection pool is being maxed out due to the context not being disposed properly (as @Adam has stated). Increase your pool settings (or decrease timeout) to test but you need to dispose of the context properly (credit goes to @Adam).

You can test this using performance counters http://msdn.microsoft.com/en-US/library/ms254503(v=VS.80).aspx

Brian Mains
  • 49,697
  • 35
  • 139
  • 249
Dustin Davis
  • 14,049
  • 12
  • 60
  • 114