ADO.NET事务提交抛出SemaphoreFullException异常

11

当我提交事务时,出现以下错误:

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;

我认为这是连接池中的一个错误。尝试禁用连接池,看看是否可以解决问题。您使用的框架版本是什么? - Pete
问题当然是真实的和值得关注的,但我只是好奇为什么你使用手动事务而不是TransactionScope - Anders Abel
@Anders 没有理由,可能应该是... - Andrew Bullock
指定连接是否解决了这个问题?你还做了什么来解决它(我问这个问题是因为我遇到了同样的问题,而且没有被接受的答案)? - ChaseMedallion
1个回答

2

正如Pete所提到的,这可能是连接池中的一个bug。无论如何,我注意到你的代码缺少了MS认为必须调用的一个函数。从MSDN中获取。

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

试试看,看它是否仍然发生。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接