Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you detect if a SqlConnection's "blocking period" is active?

Tags:

c#

ado.net

According to Microsoft's article (SQL Server Connection Pooling (ADO.NET)),

When connection pooling is enabled, and if a timeout error or other login error occurs, an exception will be thrown and subsequent connection attempts will fail for the next five seconds, the "blocking period". If the application attempts to connect within the blocking period, the first exception will be thrown again. After the blocking period ends, another connection failure by the application will result in a blocking period that is twice as long as the previous blocking period. Subsequent failures after a blocking period ends will result in a new blocking periods that is twice as long as the previous blocking period, up to a maximum of five minutes.

How would you detect that the blocking period is active? I would assume that there is some property to check before attempting the connection so that you could avoid extending the blocking period.

like image 340
okiedev Avatar asked Nov 22 '13 18:11

okiedev


People also ask

How do I check my connection pool?

From the JDBC Connection Pool—>Monitoring tab, you can view information about the state of each deployed instance of the selected connection pool. That is, for each server on which the connection pool is deployed, you can see current status information about the connection pool.

What happens when connection pool is full?

Setting the max connection pools size higher than that means you could have that many queries against the database concurrently and exacerbate problems like long-term blocking or long-running queries. Unused pooled connections are always reused before a new one is made.

What happens when max pool size is reached?

This may have occurred because all pooled connections were in use and max pool size was reached. When you receive this message, it means that your website is using all of its available SQL Database connections (the default limit is 15 connections per DotNetNuke install).


2 Answers

There shouldn't be a need to check if you're in a blocking period to avoid extending it. As it says in the excerpt above, any attempts to connect during the blocking period will re-throw the first exception, it says nothing about extending the blocking period. However, each new blocking period will be twice as long as the previous.

In my experience, the exceptions that get thrown (due to timeouts, connection leaks, etc.) are either environmental issues or failing to properly close/dispose connections. It's a good idea to log these exceptions so that you can track down the real issue.

If you do keep coming across a timeout exception, you could catch it and try to clear all the pools, but it's likely due to a connection leak. You'll want to make sure you're wrapping your connections with a using statement, which will help to close/dispose of your connections when you're done with them or if an exception occurs.

using(SqlConnection connection = new SqlConnection("connection_string"))
{
    using(SqlCommand command = new SqlCommand())
    {

        SqlCommand command = new SqlCommand();
        command.Connection = connection;
        command.CommandType = CommandType.Text;
        command.CommandTimeout = [some timeout value]; 
        command.CommandText = "Update SomeTable Set Value = 1";

        connection.Open();

        command.ExecuteNonQuery();
    }
}
like image 87
Michael Avatar answered Oct 16 '22 09:10

Michael


In addition to the ClientConnectionId field,

The SqlException.Message will also be reference-equal. That is, the cached string will be returned for connections that fail within the "blocking period".

However, this is also an implementation detail and may change.

like image 1
user2864740 Avatar answered Oct 16 '22 09:10

user2864740