1

I am having an MVC3 + Entity Framework 4.1 app, currently iam testing it locally on my pc.

I want to know whether i have closed all my connections properly by disposing entities context or not. Also is there any available method thru which i can see how many connections are currently open. I am afraid i am not closing all my open connections properly.

Or is there any other way to check whether through my web app i am closing all my db connections properly or not

Rusi Nova
  • 2,617
  • 6
  • 32
  • 48

1 Answers1

4

With ADO.NET normally you do not open/close physical database connections manually. There is a connection pool handled by the framework. So when you do new SqlConnection you are not opening a new physical connection to the database, you are simply drawing one from the pool. And when you call connection.Close you are not closing the connection, you are simply returning it to the connection pool so that it can be reused.

So what is important for you is to ensure that your code holds connections for as short time as possible and return them to the pool as fast as possible. You may take a look at the following article which goes into more details about connection pooling.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • okie: then how to make sure i am disposing dataContext currently. Actually i want to know this for for my app where iam using ninject for dependency resolution and i think ninejct is not disposing my dbContext. Please look at this question: http://stackoverflow.com/questions/7824465/how-to-dispose-dbcontextor-object-in-asp-net-mvc3-app-when-ninject-is-used-as – Rusi Nova Oct 19 '11 at 16:21
  • this answers the "what's the best practice on connection handling" but not the question that was asked here. in some cases we do want to see how many connections are in use even while properly disposing – Sonic Soul Jul 05 '17 at 18:41
  • @Darin the article link gives me a "Page not found". Do you know an updated link? – Zack Jun 20 '22 at 12:17