views:

716

answers:

2

I´m using Transaction Binding=Explicit Unbind in the connection string as recommended here since I´m also using TransactionScope with timeout. The problem is that the connections does not seem to close after being disposed and eventually there are no more connections available in the connection pool. I got the same result when I modified the TransactionTimeoutIssueDemo (see the link) and ran TransactionScopeTest() (with the explicit unbind connection string) enough times in a loop to use up all available connections in the connection pool. Default value for connections in the pool is 100 but this can be changed by using the setting Max Pool Size =10 for instance. It seems that the connections will not be released when using explicit unbind even though both the SqlConnection and the TransactionScope are used with the using clause. Anyone know how to handle this?

+1  A: 

The connections only seem to stay in the pool and not being reused in case you get an exception, just like the example. If you increase the timeout the connection will be reused.

A workaround to this problem is to clear the connection pool in case you get an exception like this:

using (SqlConnection con = new SqlConnection(connectionString))
{
    con.Open();
    try
    {
        Console.WriteLine("Server is {0}", con.ServerVersion);
        Console.WriteLine("Clr is {0}", Environment.Version);
        for (int i = 0; i < 5; i++)
        {
            using (SqlCommand cmd = con.CreateCommand())
            {
                cmd.CommandText = "insert into TXTEST values ( " + i + " )";
                cmd.ExecuteNonQuery();
            }
            Console.WriteLine("Row inserted");
        }
        Thread.Sleep(TimeSpan.FromSeconds(1));
    }
    catch
    {
        SqlConnection.ClearPool(con);
        throw;
    }
}

In most cases the transaction will complete within the timeout and everything will be fine and dandy. When the transaction actually do timeout you clear the pool in order to clean up the dirty connections that won't get reused. This will of course affect other connections in the pool that isn't affected by this problem.

This is a ugly workaround but it seems to work.

Manga Lee
A: 

For what it's worth, this issue was fixed in .Net 4.0.

Jared Moore