ado.net transaction.commit抛出semaphorefullexception

时间:2011-02-11 13:54:51

标签: c# ado.net transactions sql-server-2008-r2

当我提交交易时,我得到了:

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

这是什么意思?我是不是在某个地方正确地关闭了连接并填满了游泳池?如果是这样,我如何在SQL Server 2008 R2中检查它?

这是我的代码(虽然这可能不是导致连接泄漏的代码)

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;

1 个答案:

答案 0 :(得分:2)

正如Pete所说,这可能是连接池中的一个错误。在任何情况下,我都注意到您的代码缺少MS所说的呼叫。来自MSDN

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

尝试一下,看看它是否仍然存在。