c# - 连接泄漏可能会导致超时过期。从池中获取连接之前超时时间已过?

标签 c# .net sql-server

我收到此错误,导致我的应用程序停止工作。 Timeout expired. 在从池中获取连接之前已过超时期限。 这可能是因为所有池连接都在使用中并且已达到最大池大小。

但是我还没有达到我的最大连接池。我有 RDS,在我的监控页面中,我发现发生此错误时连接数为 33,默认情况下我的最大连接数为 100。

所以,我想知道这可能是由于我的连接泄漏所致。

这是我用来连接数据库的 DBLayer 类。

public static DataTable GetDataTable(SqlCommand command, IsolationLevel isolationLevel = IsolationLevel.ReadUncommitted)
{
    using (new LoggingStopwatch("Executing SQL " + command.CommandText, command.Parameters))
    {
        using (var connection = new SqlConnection(connectionString))
        using (var dataAdapter = new SqlDataAdapter(command))
        {
            command.Connection = connection;
            command.CommandTimeout = ShopexConfiguration.SqlTimeout;
            connection.Open();
            var transaction = connection.BeginTransaction(isolationLevel);
            command.Transaction = transaction;
            try
            {
                var result = new DataTable();
                dataAdapter.Fill(result);
                transaction.Commit();
                return result;
            }
            catch
            {
                try
                {
                    transaction.Rollback();
                }
                catch (Exception)
                {
                    //
                    // This catch block will handle any errors that may have occurred 
                    // on the server that would cause the rollback to fail, such as 
                    // a closed connection.
                }
                throw;
            }
        }
    }
}

我只是想知道,这会导致连接泄漏吗?

我看过这个博客:

https://blogs.msdn.microsoft.com/spike/2008/08/25/timeout-expired-the-timeout-period-elapsed-prior-to-obtaining-a-connection-from-the-pool/

需要任何帮助吗?

最佳答案

您确定您的查询没有达到执行超时吗? SqlConnection.ConnectionTimeout有默认值 15 秒

您的 connectionString 格式中是否也有一些连接超时值:"...;Connection Timeout=10..."

关于c# - 连接泄漏可能会导致超时过期。从池中获取连接之前超时时间已过?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39991658/

相关文章:

c# - 为什么在手指树和链表等中使用元组

c# - 如何使用代码优先方法做长文本

C# 错误 : "Fill: SelectCommand.Connection property has not been initialized."

sql-server - 查看服务器状态并查看 SQL 托管实例中的所有定义

sql-server - SSIS 到 Excel - Excel 公式没有自动运行?

c# - 如何从 MVC Controller 访问静态 System.IO.File 类?

c# - XML url 中的 & 符号未通过

c# - 从一个对象到同一实体类型的两个对象的多个关联

java - 当使用 Java Slick 2D 或 .NET XNA 框架时,用户输入如何保持独立于 FPS?

c# - 在 C# 中计算阶乘