ado.net transaction.commit 抛出 semaphorefullexception

发布于 2024-10-16 17:51:34 字数 1709 浏览 3 评论 0原文

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

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;

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温折酒 2024-10-23 17:51:34

正如皮特提到的,这可能是连接池中的一个错误。无论如何,我注意到您的代码缺少 MS 所说需要的调用。来自 MSDN

 // 必须同时分配事务对象和连接
   // 待处理本地事务的 Command 对象
   命令.Connection = 连接;
   命令.事​​务 = 事务;

尝试一下,看看它是否仍然会发生。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文