Max SQL Connections Configuration in .NET Application
Recently I ran into a problem with running out of database connections in a .NET web application. The issue became manifest during load testing, as our company was preparing for its annual conference. We were anticipating many hundreds of people accessing a portion of the website at once, filling out assessment forms. The issue became apparent as our DAL (Data access layer) library was performing a lot of processing. This would be fine except for the fact that while the processing was occurring filling out our domain objects, it kept a connection to the database open. It turns out that the default max number of connections you can have open at a time is 100, unless you use the Max Pool Size property of a connection string:
public static string srConnectionString = "server=localhost;database=mydb;uid=sa;pwd=mypw;Max Pool Size=200;";
After increasing this, it helped alleviate the connection error, however we wanted to ensure a high quality application, and in reality removed the processing from the DAL and closed the connection before de-normalizing the detailed collection of domain objects from the database. This leaves me to the following conclusion about dealing with database connections:
1.) That you either minimize the time you have connections open or 2.) You keep one connection open for multiple queries or users
You run in to problems however if you choose option 1 and do much processing with the connection open. This in reality could be looked at as a configuration problem, not a resource problem, as if there is no CPU spike, memory thrashing, or hard disk I/O bottlenecks, simply increasing the pool size would do the trick.