数据库
首页 > 数据库> > 跨环境随机且很少出现SQL Server“找不到网络路径”

跨环境随机且很少出现SQL Server“找不到网络路径”

作者:互联网

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在这个问题上无济于事,这最终导致了该应用程序的出现,该应用程序在开发环境中得以重现.

连接字符串比较平淡,形式为“数据源=;数据库=;用户ID =;密码=”

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

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

“选择1 Field1,2 Field2,3 Field3”

该测试确实使用Nito.AsyncEx(这里使用的唯一的系统程序集)来获取异步支持.同样,该问题在其他未使用该程序集的应用程序中也会发生,因此我认为这不是问题-如果没有请让我知道,我将以另一种方式重现.

任何想法,不胜感激!

解决方法:

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

一些相关案例:

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

结论,除非SQL Server位于同一台计算机上,否则请始终明确使用TCP / IP.您可以将SQL Server配置为也不接受命名管道,但是将来我也将其添加到连接字符串中.

标签:ado-net,networking,named-pipes,c,sql-server
来源: https://codeday.me/bug/20191026/1935012.html