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

时间:2016-10-12 06:15:06

标签: 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/

感谢任何帮助?

2 个答案:

答案 0 :(得分:1)

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

connectionString 格式中还有一些连接超时值是:" ....;连接超时= 10 ..."

答案 1 :(得分:1)

我认为您应该在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

希望它会对你有所帮助。

由于