Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ado.net transaction.commit throws semaphorefullexception

When I commit my transaction, i'm getting:

System.Threading.SemaphoreFullException: Adding the specified count to the semaphore would cause it to exceed its maximum count.
   at System.Threading.Semaphore.Release(Int32 releaseCount)
   at System.Data.ProviderBase.DbConnectionPool.PutNewObject(DbConnectionInternal obj)
   at System.Data.ProviderBase.DbConnectionPool.DeactivateObject(DbConnectionInternal obj)
   at System.Data.ProviderBase.DbConnectionPool.PutObject(DbConnectionInternal obj, Object owningObject)
   at System.Data.ProviderBase.DbConnectionInternal.CloseConnection(DbConnection owningObject, DbConnectionFactory connectionFactory)
   at System.Data.SqlClient.SqlConnection.Close()
   at System.Data.SqlClient.SqlConnection.Dispose(Boolean disposing)
   at System.ComponentModel.Component.Dispose()
   // rest of my stack trace here

What does this mean? Am I not closing a connection properly somewhere and have filled the pool up? If so, how do I check this in SQL Server 2008 R2?

here's my code (although this may not be the code guilty of causing the connection leak)

using (var connection = connectionFactory.GetConnection())
{
    connection.Open();

    using (var transaction = connection.BeginTransaction())
    {
        try
        {
            using (var command = connection.CreateCommand())
            {
                command.Connection = connection;
                command.Transaction = transaction;
                command.CommandText = "some sql"

                data = (string) command.ExecuteScalar();

                transaction.Commit();
            }
        }
        catch
        {
            try
            {
                transaction.Rollback();
            }
            catch
            {
            }
            throw;
        }
    }
}

return data;
like image 946
Andrew Bullock Avatar asked Feb 11 '11 13:02

Andrew Bullock


1 Answers

As Pete mentioned, this might be a bug in connection pooling. In any case, I noticed your code is missing a call that MS says is required. From MSDN

   // Must assign both transaction object and connection
   // to Command object for a pending local transaction
   command.Connection = connection;
   command.Transaction = transaction;

Give that a try and see if it still happens.

like image 111
Brad Divine Avatar answered Nov 10 '22 11:11

Brad Divine