views:

94

answers:

3

We've recently moved our company website to a new host. It is an ASP.NET site with C# code behind, connecting to MS SQL server.

Since moving the site to the new server, the site is exceeding the connection pool limit (which is not set explicitly, so I believe is at the default size of 100). Inspection of the open processes via SQL Server Management Studiorevealed that every database call appeared to be left open, and indeed, in the code I can find no explicitly closed connections at all.

The database connections are made in the following way:

DSLibrary.DataProviders.SqlProvider db = new DSLibrary.DataProviders.SqlProvider(Defaults.ConnStr);

There is precious little documentation about this DSLibrary and I assume that it's a library written by the original developer of the website. None of the class members of DSLibrary appear to explicitly close the connections either - nor are they defined within a using block to automatically close the connection.

My question is 2 fold.

  1. How would we have not encountered this problem when the site was on a different host for nearly 3 years? Is there away to automatically close unused connections that I have not implemented on the SQL server?
  2. Will I be best off rewriting every connection and procedure to explicitly open and close the connection database?

UPDATE The maximum number of concurrent connections property (Server Properties -> Connections tab) is set to 0.

If I run the website in debug mode on my development machine, connecting remotely to the production database, then the connections seem to close properly. This seems to show it's something to do with the way IIS is configured?

UPDATE 2 Setting the application pool to recycle after 30 worker processes has stopped the site exceeding maximum connections, but is now limiting some (session persistent) functionality - a recently visited items list resets very quickly and attempting to edit anything via the cms is impossible as you are logged out as soon as the processes recycle...

A: 

Have you checked the maximum number of concurrent connections property (Server Properties -> Connections tab)?

slugster
I have, and it is set at 0 (unlimited)
fearoffours
+3  A: 

Most likely your code is leaking connections all over the place.

I'd bet money your old host had the app pool set to recycle pretty often, either from a memory usage or # requests processed point. The new host most likely has default cycling in place.

My recommendation is to first set the app pool to recycle a lot more often. Then fix the code. Either by refactoring out the DSLibrary (guessing that's home grown) or by simply changing it over to using clauses whereever you have database connections made.

update
One more thing, change your session properties to use sql server as the backing store that way you won't lose all the session information as the app recycles. This will buy you more time.

Chris Lively
Thanks Chris, this was helpful. I've set the application pool to recycle after 30 worker processes and this seems to be keeping the problem at bay. However I note now that because of this, our "Recently viewed items" only retains those recently viewed product until the processes recycle. I understand why this happens, but again wonder why this didn't happen on the previous server.Also, yes I believe the DSLibrary is homegrown. The connections are all handled internally by this library, not sure how I'll be able to add the `using` clauses
fearoffours
If you still have access to the other server, go through the IIS properties of the app pool and site and see what else might be different. Did you happen to change IIS versions in the move?
Chris Lively
unfortunately, I have never had access to IIS settings on the previous server. This was one of therebr for the note in the first place. i don't know what the previous IIS version was, so a change is likely. It seems I will be rewriting the connections code even more urgently than I expected.
fearoffours
@fearoffours: One other thing, if its session related, you might consider changing the session properties to use sql server instead of in-process. That way it will survive restarts.
Chris Lively
I've attempted to fix the code, explicitly closing connections. I'm having problems, please see http://stackoverflow.com/questions/3100963/am-i-closing-this-sql-connection-in-a-c-function-appropriately
fearoffours
A: 

From what you say you don't appear to be wrapping the database in a using statement so you're leaking a connection each time. The system dumps the stuff when it gets around to it unless you explicitly dispose it--which is the purpose of using. You must always use a using (or otherwise ensure dispose is called) on anything that represents a resource outside your program. Files, database connections etc.

Loren Pechtel
I absolutely agree this is what I need to do, but the fact that it was working on a previous server with no code changes, indicates there was some (unsatisfactory) workaround.
fearoffours