Sql server 零星SQL连接超时

Sql server 零星SQL连接超时,sql-server,Sql Server,我们在连接到远程安装的MSSQL 2012实例的计算机上部署了一些应用程序。除一个应用程序外,它们以Windows服务的形式在本地管理员帐户下运行。 客户端运行在32位Windows 7上,数据库运行在64位Windows Server 2008 R2上 我在四次连接尝试中有一次出现此错误: SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a c

我们在连接到远程安装的MSSQL 2012实例的计算机上部署了一些应用程序。除一个应用程序外,它们以Windows服务的形式在本地管理员帐户下运行。 客户端运行在32位Windows 7上,数据库运行在64位Windows Server 2008 R2上

我在四次连接尝试中有一次出现此错误:

SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 0 - A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
   bei System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
   bei System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
   bei System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity)
   bei System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, SqlConnection owningObject)
   bei System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, TimeoutTimer timeout)
   bei System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, TimeoutTimer timeout, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance)
   bei System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance)
   bei System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection)
   bei System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options)
   bei System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject)
   bei System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject)
   bei System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
   bei System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
   bei System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
   bei System.Data.SqlClient.SqlConnection.Open()
我在所有应用程序中使用此连接字符串:

数据源=192.168.0.101\NAMEDINSTANCE;初始目录=数据库;综合安全=假;持久安全信息=False;用户ID=用户;密码=密码

我用无限ping请求检查了网络连接,但出现错误时,网络连接正常。 首先,我得到了一条类似的错误消息,虽然NamedPipes已启用,但在停用NamedPipes(我们不使用它)后,上述错误以相同的频率发生。 同样的设置也适用于其他机器。 在它成功运行大约3个月之前,错误在几周前开始出现。这是一个离线设置,自部署以来系统没有更新(但我不能保证)。 两台机器上的防火墙都已关闭。 Windows或SQL Server事件日志上没有提示。 我还更换了网络电缆,以排除电缆断裂

我现在有点困了,因为我不知道下一步该去哪里。可能对connectionstring进行了一些调整,或者我可以更改服务器或客户端的某些配置

更新: 在GranadCoders的帮助下,我发现tcp调用可能会被某些东西过滤掉。我用PortQry检查了一下

portqry -n 192.168.0.101 -e 1433 -p tcp
TCP port 1433 (ms-sql-s service): FILTERED
如果这种情况一直发生,我会说防火墙正在阻止我的呼叫,但在这种情况下,可能是某种系统过载导致我的请求丢失。 接下来要做的事情是通过连接特定端口来检查是否是SQL Server浏览器故障。然后更换开关

更新(2013-07-03):我编写了一个测试应用程序,每秒打开一个连接,并在出现故障的系统上运行。成功了。在几个连接中,我在其他应用程序上出现了两次错误,但在我的测试应用程序的500个连接中,我没有成功地出现一次错误

更新(2013-07-05:我忘了有另一个网络连接到此服务器。该连接是在几周前连接的。看起来这是此问题的根本原因

  • 阅读
  • 您可以定义要使用的网络库。这是我的目标
  • 可能将端口添加到连接字符串中
  • 当您使用IP地址时,请指定端口号并将其提供给网络库,我认为这是您可以得到的“非常非常具体的”


    查看示例。

    很难说这是从哪里来的,但是使用连接字符串中的端口而不是命名实例是一个选项吗


    如果使用命名实例,第一个请求将转到目标系统上的SQL Server浏览器(在端口1434上运行)获取与命名实例关联的端口。只有这样,才能真正连接到指定的端口。如果您直接使用该端口,则此往返将被消除。如果您在任何地方都使用该端口,则还可以通过关闭SQL Server浏览器来减少攻击面。

    请检查您的开发人员是否始终使用该端口一个
    try/finally
    或一个
    使用
    块进行连接。您经常会看到这样的代码:

    SqlConnection con = ... ;
    con.Open();
    
    // ... use the connection ...
    
    con.Close();
    

    该代码为<强>错误< /强>。如果在该代码的中间发生异常,则<代码> CON.CULL()line永远不会执行。这会导致连接保持打开状态,如果这种情况经常发生,则会将服务器上允许的活动连接数设为最大,将自己锁定在服务器之外,并获得与您发布的连接类似的异常跟踪

    相反,您需要始终使用
    块或
    try来包装连接

    using (SqlConnection con = ... )
    {
        con.Open();
    
        // ... use the connection ...
    }
    
    请注意,我甚至不需要在这里调用
    con.Close()
    ,因为using块为我处理了它。另一个选项:

    SqlConnection con = ... ;
    try 
    {    
        con.Open();
    
        // ... use the connection ...
    }
    finally
    {
        con.Close();
    }
    

    服务器和客户端都通过两个独立的网络连接。当我通过网络a发送连接请求时,SQL server似乎可以通过网络B进行响应

    在Windows中正确设置了网络优先级(A优于B)

    解决我的问题的方法是更换服务器上的两条网线。现在不再超时,一切正常。 服务器主板上有两个连接器,但似乎忽略了Windows的优先级设置


    网络B在SQL Server TCP配置中被禁用,因此无论如何它都不应该使用它。这对我来说是一种奇怪的行为,因为我找不到发生这种情况的原因。

    为什么要在这上面标记C?但你所谈论的都是网络、SQL Server和策略,所以除非是特定的C?问题,请不要标记C。@JoelCoehoorn,这几乎肯定是一个网络配置或群集服务器问题。不太可能需要更改C#代码,甚至SQL server实例本身。最多,他们可能需要更改连接字符串。但我见过这种情况数千次,90%的时间是pr问题是网络上有东西阻塞了连接,另外10%通常是群集/服务器场无法正确服务请求。你可能知道,但OP可能不知道。是的,在验证这确实不是C#问题后,重新标记问题,但不要因为OP标记错误而大发雷霆f故障排除的第一条规则是不要忽略任何可能导致问题的因素。我再次删除了C#标记,因为它会将答案引入错误的方向