locked
Database connections not closing RRS feed

  • Question

  • We have had a strange problem occur and I am hoping someone will know why.  We have an application we converted from .NET 1.1 to .NET 2.0.  In the web application we open our connection on page load and then close it on page unload.  If you step through the code all events appear to fire.  Under 1.1 we never had any problems.  When we deployed the 2.0 version things went crazy.  I monitored the numberofreclaimedconnections counter and this number went through the roof basically no connections were being closed by the app it apperaed the GC was handling all of them when we were under load all the connections were eaten up and the app would error.  To fix this problem we went through the code and changed all the connections to open and close immediately after use, which I know is the recommended best practice.  But my questions is why was the close not occuring when we did it the other way? 
    Maria
    Thursday, June 12, 2008 6:20 PM

Answers

  • (in 1.1 that did not exist that is why it was not in there to start)

    The Using block was in C# since early 2001 which is .NET 1.0 so if your code is in C# who ever wrote the code may not be C# skilled just writes it.  And the other reason could be Asp.net 2.o comes with Master Pages, themes and Navigation which are very early in the HTTP pipeline all must be added at PreInit or your code will not run.


    http://www.codeproject.com/KB/aspnet/lifecycle.aspx


    An Elliptic curve is modular ~ Taniyama and Shimura
    • Marked as answer by Bruno Yu Tuesday, June 17, 2008 5:44 AM
    Friday, June 13, 2008 2:37 AM

All replies

  • It depends if you are using dataset the only thing that implements IDisposable in the dataset is the connection object so if you created command objects and did not dispose of the connection object your dataset remain open.  I have also seen memory problems and scalability issues with datasets which the connection object is not disposed.  That is the reason the Asp.net team wants developers to know the second using statement in C# and the using statement in VB.NET 2.


    An Elliptic curve is modular ~ Taniyama and Shimura
    Thursday, June 12, 2008 6:48 PM
  • We were disposing all of the connections and command objects
    Maria
    Thursday, June 12, 2008 7:00 PM
  • Dispose is only relevant to the connection object not the command object because the command objects did not implement IDisposable.  The best practice in Asp.net is to wrap all database related operations in a Using block so the clr will call dispose for you automatically.  So your problem could be related to duration from page load to page unload as related to how busy is the site.


     


    An Elliptic curve is modular ~ Taniyama and Shimura
    • Proposed as answer by Bruno Yu Tuesday, June 17, 2008 5:45 AM
    Thursday, June 12, 2008 7:35 PM
  • Right, that is what we have done, in 1.1 that did not exist that is why it was not in there to start, my question is why did this problem occur did something in the framework change
    Maria
    Thursday, June 12, 2008 10:13 PM
  • (in 1.1 that did not exist that is why it was not in there to start)

    The Using block was in C# since early 2001 which is .NET 1.0 so if your code is in C# who ever wrote the code may not be C# skilled just writes it.  And the other reason could be Asp.net 2.o comes with Master Pages, themes and Navigation which are very early in the HTTP pipeline all must be added at PreInit or your code will not run.


    http://www.codeproject.com/KB/aspnet/lifecycle.aspx


    An Elliptic curve is modular ~ Taniyama and Shimura
    • Marked as answer by Bruno Yu Tuesday, June 17, 2008 5:44 AM
    Friday, June 13, 2008 2:37 AM
  •  Our code is an asp.net app written in vb.net so using did not exist until recently, our pages are not using master pages or any of the new 2.0 features, but could that still be causing this.
    Maria
    Friday, June 13, 2008 6:08 PM