繁体   English   中英

连接泄漏是否会导致Timeout过期。 从池中获取连接之前是否已经过了超时时间?

[英]Will connection leak might Cause Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool?

我收到此错误导致我的应用程序停止工作。 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格式中还有一些连接超时值是: “......;连接超时= 10 ......”

我认为你应该在你的finally语句中关闭你的连接,如下所示:

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.
                }
                finally { connection.Close(); }
                throw;
            }
        }
    }
}

也许它可以解决您的问题,因为您在catch语句中回滚事务而不关闭现有连接,您也可以在打开SQL连接之前使用以下条件:

    if (connection.State == ConnectionState.Closed) {
        // Open your connection here
        connection.Open();
    }
// Do your logic here

希望它能帮到你。

谢谢

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM