SQL Server 主动防止阻塞的 1 方法

时间:2023-03-09 06:35:41
SQL Server 主动防止阻塞的 1 方法

方法 1、

set lock_timeout 5000;  这里设置超时为5秒;

例子:

连接A

begin tran
             update dbo.TestTable
             set String = 'AAA'
             where ID=1;
             go

连接B

update dbo.TestTable
             set String = 'BBB'
             where ID=1;
             go

SQL Server 主动防止阻塞的 1 方法

可以看到不设置 lock_timeout 它是会一直等下去的。

连接C

set lock_timeout 5000;

update dbo.TestTable
             set String = 'BBB'
             where ID=1;
             go

SQL Server 主动防止阻塞的 1 方法

可以看到只要等5s超时就不等了、问题来了,

连接C的下一个批处理也是5s超时吗?我们来测一下。

SQL Server 主动防止阻塞的 1 方法

还是5s说明这个set lock_timeout是对连接有效的,不是只对单个的批处理。