While the MSDN article says that the event will only be raised for non-reused connections, the SQL Server documentation contradicts this statement:
"The Audit Login event class indicates that a user has successfully logged in to Microsoft SQL Server. Events in this class are fired by new connections or by connections that are reused from a connection pool."
The best way to measure the effectiveness of pooling is to collect the time spent in connecting with and without pooling. With pooling, you should see that the first connection is slow and the subsequent ones are extremely fast. Without pooling, every connection will take a lot of time.
If you want to track the Audit Logon event, you can use the EventSubClass data column to whether the login is with a reused connection or a new connection. The value will be 1 for a real connection and 2 for a reused connection from the pool.application.
In most cases connection pooling problems are related to connection leaks. Your application probably doesn't close its database connections correctly and consistently. When you leave connections open, they remain blocked until the .NET garbage collector closes them for you by calling their Finalize()
method.
You want to make sure that you are really closing the connection. For example the following code will cause a connection leak, if the code between .Open
and Close
throws an exception:
var connection = new SqlConnection(connectionString);
connection.Open();
// some code
connection.Close();
The correct way would be this:
var connection = new SqlConnection(ConnectionString);
try
{
connection.Open();
someCall (connection);
}
finally
{
connection.Close();
}
or
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
someCall(connection);
}
When your function returns a connection from a class method make sure you cache it locally and call its Close
method. You'll leak a connection using this code for example:
var command = new OleDbCommand(someUpdateQuery, getConnection());
result = command.ExecuteNonQuery();
connection().Close();
The connection returned from the first call to getConnection()
is not being closed. Instead of closing your connection, this line creates a new one and tries to close it.
If you use SqlDataReader
or a OleDbDataReader
, close them. Even though closing the connection itself seems to do the trick, put in the extra effort to close your data reader objects explicitly when you use them.
This article "Why Does a Connection Pool Overflow?" from MSDN/SQL Magazine explains a lot of details and suggests some debugging strategies:
- Run
sp_who
or sp_who2
. These system stored procedures return information from the sysprocesses
system table that shows the status of and information about all working processes. Generally, you'll see one server process ID (SPID) per connection. If you named your connection by using the Application Name argument in the connection string, your working connections will be easy to find.
- Use SQL Server Profiler with the SQLProfiler
TSQL_Replay
template to trace open connections. If you're familiar with Profiler, this method is easier than polling by using sp_who.
- Use the Performance Monitor to monitor the pools and connections. I discuss this method in a moment.
- Monitor performance counters in code. You can monitor the health of your connection pool and the number of established connections by using routines to extract the counters or by using the new .NET PerformanceCounter controls.
Best Answer
There is a way for a connection to remain open when using SubSonic. Many people assume that when you load a collection the reader will be closed for you - but it's not (a class should never act on another class without permission). There is a method called "LoadAndCloseReader()" just for this reason.
If you could find out more that would be great.