sqlserver 连接超时设置

2023-02-20 00:00:00 服务器 等待 实例 链接 右键

我们经常看到MSSQL服务器参数配置及 SSMS 中有很多地方设置超时,但是这些参数即使设置成1秒钟,执行各种查询似乎也正常。所以完全不知道有什么用(疑惑脸……)。之前有遇到也没明白,今晚刚好利用公司升级时间,摸索测试出来了!(豁然开朗~)


不废话了,首先我们得知道,超时影响的是客户端,也是客户端的行为导致。所以我们看到的或者在其他程序语言中设置的,都是客户端的设置。那 SQL Server 客户端哪里可以设置呢? 打开 SSMS ( microsoft sql server management studio),现在就把它当做客户端,所以会看到各种超时设置。


哪里可以看到??

SELECT * FROM sys.configurations WHERE configuration_id IN (1519,1520,1541);

这里,3个
remote login timeout (s)
remote query timeout (s)
query wait (s)

还有!慢慢体会吧?乍一看,好像都差不多!~

(右键实例) >> Server Properties >> connections >> remote query timeout
(右键实例) >> Server Properties >> Advanced >> remote login timeout
(右键实例) >> Server Properties >> Advanced >> query wait

(右键链接服务器) >> 属性 >> 服务器选项 >> query timeout
(右键链接服务器) >> 属性 >> 服务器选项 >> connection timeout

Tools >> Options >> Query Execution >> SQL Server >> General >> Execution Time-Out
Tools >> Options >> Query Execution >> SQL Server >> Advanced >> SET LOCK TIMEOUT

--先打开一个查询窗口才看到菜单“Query”
Query >> Query Options >> Execution >> General >> Execution Time-Out
Query >> Query Options >> Execution >> Advanced >> Execution SET LOCK TIMEOUT

SELECT @@LOCK_TIMEOUT

一开始,凌乱~~所以就分类如下:
-- 对链接服务器有效,客户端连接服务器长等待时间
exec sp_configure 'remote login timeout (s)'
(右键实例) >> Server Properties >> Advanced >> remote login timeout
(右键链接服务器) >> 属性 >> 服务器选项 >> connection timeout

-- 对链接服务器有效,客户端执行服务器语句前长等待时间
exec sp_configure 'remote query timeout (s)'
(右键实例) >> Server Properties >> connections >> remote query timeout
(右键链接服务器) >> 属性 >> 服务器选项 >> query timeout

-- 对链接服务器有效,查询等待内存资源时间(这模拟不到测试,放弃测试)
exec sp_configure 'query wait (s)'
(右键实例) >> Server Properties >> Advanced >> query wait

-- 等待锁获取到资源前的长等待时间
SET LOCK_TIMEOUT 30000; --单位毫秒
Query >> Query Options >> Execution >> Advanced >> Execution SET LOCK TIMEOUT
Tools >> Options >> Query Execution >> SQL Server >> Advanced >> SET LOCK TIMEOUT

-- 执行语句前的长等待时间
Query >> Query Options >> Execution >> General >> Execution Time-Out
Tools >> Options >> Query Execution >> SQL Server >> General >> Execution Time-Out

一种情况之所以有几种设置,是因为在不同地方都可以设置,有的还可以设置局部。

【组】

--当前实例的全局配置,两种设置都是同一个参数。所有链接服务器的默认链接等待超时值。
exec sp_configure 'remote login timeout (s)'
(右键实例) >> Server Properties >> Advanced >> remote login timeout

--该链接服务器的链接等待超时值。
(右键链接服务器) >> 属性 >> 服务器选项 >> connection timeout

测试前,配置链接服务器。俩个实例如 A 和 B,其中一个实例 A 创建链接服务器连接到 B 服务器。
现在,手动停止 B 服务器中的数据库服务器!

在 A 服务器实例执行链接服务器的查询,15秒等待后报错。


OLE DB provider "SQLNCLI10" for linked server "WIN-AHAU9NO5R6U,14333" returned message "Login timeout expired".
OLE DB provider "SQLNCLI10" for linked server "WIN-AHAU9NO5R6U,14333" returned message "A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.".
Msg 10061, Level 16, State 1, Line 0
TCP Provider: 由于目标计算机积极拒绝,无法连接。

现在,在A服务器更改远程连接超时值为 5 秒!在查询

exec sp_configure 'remote login timeout (s)',5
reconfigure with override
go
SELECT * FROM [WIN-AHAU9NO5R6U,14333].master.sys.objects


此时发现,在连接成功前,等待5秒就停止了!


本文来源https://blog.csdn.net/kk185800961/article/details/52969608

相关文章