c# - SQL Server "Network Path Not Found"跨环境随机且不频繁发生

标签 c# sql-server networking ado.net named-pipes

类似(如果不是同一个问题)Network path not found exception encountered randomly ,但我有重现该问题的代码,所以我想再问一次,因为它似乎是独立于硬件的真实问题并且可以重现。

这是错误:

provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) ---> System.ComponentModel.Win32Exception (0x80004005): The network path was not found at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionPool.WaitForPendingOpen()

为了重现这一点,我创建了一个每分钟运行一次的控制台应用程序(我们还有一个 Dapper DAL 测试,因此有参数):

internal class Program
{
    private static int _totalOpenConnections;
    private static readonly Stopwatch Timer = new Stopwatch();
    private static bool _hasError;

    private static int Main(string[] args)
    {
        var list = Enumerable.Range(1, Settings.Default.TotalCommandsToExecute);

        // simple ADO.NET test
        if (args.Length > 0 && args[0].Equals("ado", StringComparison.OrdinalIgnoreCase))
        {
            Console.WriteLine("Beginning ADO.NET Test...");

            Timer.Restart();

            Parallel.ForEach(list, new ParallelOptions {MaxDegreeOfParallelism = Settings.Default.ConcurrentCount},
                i => AsyncContext.Run(async () =>
                {
                    try
                    {
                        PrintStatus(i);
                        await TestADONet();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.ToString());
                        _hasError = true;
                    }
                }));

            Timer.Stop();

            Console.WriteLine($"Completed ADO.NET Test in {Timer.ElapsedMilliseconds} ms");
        }

        if (_hasError)
            return 1;
        return 0;
    }

    private static void PrintStatus(int index)
    {
        Console.WriteLine(
            $"Started: {index} of {Settings.Default.TotalCommandsToExecute}\tCurrently Open: {_totalOpenConnections}");
    }

    private static async Task TestADONet()
    {
        using (var conn = new SqlConnection(Settings.Default.TestConnection))
        {
            await conn.OpenAsync();
            Interlocked.Increment(ref _totalOpenConnections);

            var command = new SqlCommand("SELECT 1 Field1, 2 Field2, 3 Field3", conn);
            var reader = await command.ExecuteReaderAsync();
            while (reader.Read())
            {
                var result = new TestEntity
                {
                    Field1 = reader.GetInt32(0),
                    Field2 = reader.GetInt32(1),
                    Field3 = reader.GetInt32(2)
                };
            }
        }
        Interlocked.Decrement(ref _totalOpenConnections);
    }

    public class TestEntity
    {
        public int Field1 { get; set; }

        public int Field2 { get; set; }

        public int Field3 { get; set; }
    }
}

应用程序设置 ConcurrentCount = 100 和 TotalCommandsToExecute = 200。这个想法是通过并行异步命令非常努力地访问连接池。

此应用程序复制它,但是,它也出现在控制台应用程序、Web 应用程序(ASP.NET MVC 和 ASP.NET WebForms)的生产中。

它也相当随机地发生。我们已经让 Rackspace 和一些 DBA 就这个问题在环境中爬行,但无济于事,这导致了这个应用程序 - 它在开发环境中复制了它。

连接字符串相当平淡,形式为“Data Source=;Database=;User Id=;Password="

SQL Server 2014,但这发生在两个独立的服务器(dev/rackspace)上

测试中的查询是故意良性的

“SELECT 1 Field1, 2 Field2, 3 Field3”

该测试确实使用了 Nito.AsyncEx,这是这里唯一使用的非系统程序集,以获得异步支持。该问题再次发生在其他不使用此程序集的应用程序中,因此我认为这不是问题 - 如果不是,请告诉我,我将以其他方式重现它。

非常感谢任何想法!

最佳答案

问题出在命名管道上。它可能在 VM 中表达得更多(来自以下链接的推测)。通过在连接字符串中添加 tcp: 并指定端口来使用 TCP/IP 解决了该问题。

一些相关案例:

  1. https://dba.stackexchange.com/questions/24165/sql-server-should-we-use-tcp-or-named-pipes-or-use-the-default
  2. http://devproconnections.com/database-development/sql-server-performance-tip-favoring-tcpip-over-named-pipes
  3. https://serverfault.com/questions/30955/sporatic-connection-issues-connecting-to-sql-2005-named-pipes-vs-tcp-ip-issue/31024#31024
  4. https://dba.stackexchange.com/questions/29106/named-pipes-connection-closed-when-subjected-to-load

结论,始终明确使用 TCP/IP,除非 SQL Server 在同一台机器上。您也可以将 SQL Server 配置为不接受命名管道,但今后我也将把它添加到我的连接字符串中。

关于c# - SQL Server "Network Path Not Found"跨环境随机且不频繁发生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42259201/

相关文章:

c# - 使用 Objective Sharpie 绑定(bind) CocoaPods 库

sql-server - 无法连接到 SQL Server Management Studio

sql-server - 将一列转换为 json

SQL SERVER 事务日志已满

networking - 通过 SFTP 将文件发送到 Docker 容器

c - 如何在 C 中模拟套接字

c# - 在 TransactionScope 范围内打开新的数据库连接,而不加入事务

c# - 无法通过 TCP 连接连接到 Android

C# WPF DatePicker 绑定(bind)

android - 本地 iOS-Android zeroconf ad-hoc 网络