天天看点

[sql server] 锁知识

未整理。

--1

虽然不能完全避免死锁,但可以使死锁的数量减至最少。将死锁减至最少可以增加事务的吞吐量并减少系统开销,因为只有很少的事务:

回滚,而回滚会取消事务执行的所有工作。

由于死锁时回滚而由应用程序重新提交。

下列方法有助于最大限度地降低死锁:

按同一顺序访问对象。

避免事务中的用户交互。

保持事务简短并在一个批处理中。

使用低隔离级别。

使用绑定连接。

按同一顺序访问对象

如果所有并发事务按同一顺序访问对象,则发生死锁的可能性会降低。例如,如果两个并发事务获得 Supplier 表上的锁,然后获得 Part 表上的锁,则在其中一个事务完成之前,另一个事务被阻塞在 Supplier 表上。第一个事务提交或回滚后,第二个事务继续进行。不发生死锁。将存储过程用于所有的数据修改可以标准化访问对象的顺序。

避免事务中的用户交互

避免编写包含用户交互的事务,因为运行没有用户交互的批处理的速度要远远快于用户手动响应查询的速度,例如答复应用程序请求参数的提示。例如,如果事务正在等待用户输入,而用户去吃午餐了或者甚至回家过周末了,则用户将此事务挂起使之不能完成。这样将降低系统的吞吐量,因为事务持有的任何锁只有在事务提交或回滚时才会释放。即使不出现死锁的情况,访问同一资源的其它事务也会被阻塞,等待该事务完成。

保持事务简短并在一个批处理中

在同一数据库中并发执行多个需要长时间运行的事务时通常发生死锁。事务运行时间越长,其持有排它锁或更新锁的时间也就越长,从而堵塞了其它活动并可能导致死锁。

保持事务在一个批处理中,可以最小化事务的网络通信往返量,减少完成事务可能的延迟并释放锁。

使用低隔离级别

确定事务是否能在更低的隔离级别上运行。执行提交读允许事务读取另一个事务已读取(未修改)的数据,而不必等待第一个事务完成。使用较低的隔离级别(例如提交读)而不使用较高的隔离级别(例如可串行读)可以缩短持有共享锁的时间,从而降低了锁定争夺。

使用绑定连接

使用绑定连接使同一应用程序所打开的两个或多个连接可以相互合作。次级连接所获得的任何锁可以象由主连接获得的锁那样持有,反之亦然,因此不会相互阻塞

检测死锁

如果发生死锁了,我们怎么去检测具体发生死锁的是哪条SQL语句或存储过程?

这时我们可以使用以下存储过程来检测,就可以查出引起死锁的进程和SQL语句。SQL Server自带的系统存储过程sp_who和sp_lock也可以用来查找阻塞和死锁, 但没有这里介绍的方法好用。

use master

go

create procedure sp_who_lock

as

begin

declare @spid int,@bl int,

 @intTransactionCountOnEntry  int,

        @intRowcount    int,

        @intCountProperties   int,

        @intCounter    int

 create table #tmp_lock_who (

 id int identity(1,1),

 spid smallint,

 bl smallint)

 IF @@ERROR<>0 RETURN @@ERROR

 insert into #tmp_lock_who(spid,bl) select  0 ,blocked

   from (select * from sysprocesses where  blocked>0 ) a

   where not exists(select * from (select * from sysprocesses where  blocked>0 ) b

   where a.blocked=spid)

   union select spid,blocked from sysprocesses where  blocked>0

 IF @@ERROR<>0 RETURN @@ERROR

-- 找到临时表的记录数

 select  @intCountProperties = Count(*),@intCounter = 1

 from #tmp_lock_who

 IF @@ERROR<>0 RETURN @@ERROR

 if @intCountProperties=0

  select '现在没有阻塞和死锁信息' as message

-- 循环开始

while @intCounter <= @intCountProperties

begin

-- 取第一条记录

  select  @spid = spid,@bl = bl

  from #tmp_lock_who where Id = @intCounter

 begin

  if @spid =0

            select '引起数据库死锁的是: '+ CAST(@bl AS VARCHAR(10)) + '进程号,其执行的SQL语法如下'

 else

            select '进程号SPID:'+ CAST(@spid AS VARCHAR(10))+ '被' + '进程号SPID:'+ CAST(@bl AS VARCHAR(10)) +'阻塞,其当前进程执行的SQL语法如下'

 DBCC INPUTBUFFER (@bl )

 end

-- 循环指针下移

 set @intCounter = @intCounter + 1

end

drop table #tmp_lock_who

return 0

end

杀死锁和进程

如何去手动的杀死进程和锁?最简单的办法,重新启动服务。但是这里要介绍一个存储过程,通过显式的调用,可以杀死进程和锁。

use master

go

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_killspid]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)

drop procedure [dbo].[p_killspid]

GO

create proc p_killspid

@dbname varchar(200)    --要关闭进程的数据库名

as 

    declare @sql  nvarchar(500) 

    declare @spid nvarchar(20)

    declare #tb cursor for

        select spid=cast(spid as varchar(20)) from master..sysprocesses where dbid=db_id(@dbname)

    open #tb

    fetch next from #tb into @spid

    while @@fetch_status=0

    begin 

        exec('kill '[email protected])

        fetch next from #tb into @spid

    end 

    close #tb

    deallocate #tb

go

--用法 

exec p_killspid  'newdbpy'

查看锁信息

如何查看系统中所有锁的详细信息?在企业管理管理器中,我们可以看到一些进程和锁的信息,这里介绍另外一种方法。

--查看锁信息

create table #t(req_spid int,obj_name sysname)

declare @s nvarchar(4000)

    ,@rid int,@dbname sysname,@id int,@objname sysname

declare tb cursor for

    select distinct req_spid,dbname=db_name(rsc_dbid),rsc_objid

    from master..syslockinfo where rsc_type in(4,5)

open tb

fetch next from tb into @rid,@dbname,@id

while @@fetch_status=0

begin

    set @s='select @objname=name from ['[email protected]+']..sysobjects where [email protected]'

    exec sp_executesql @s,N'@objname sysname out,@id int',@objname out,@id

    insert into #t values(@rid,@objname)

    fetch next from tb into @rid,@dbname,@id

end

close tb

deallocate tb

select 进程id=a.req_spid

    ,数据库=db_name(rsc_dbid)

    ,类型=case rsc_type when 1 then 'NULL 资源(未使用)'

        when 2 then '数据库'

        when 3 then '文件'

        when 4 then '索引'

        when 5 then '表'

        when 6 then '页'

        when 7 then '键'

        when 8 then '扩展盘区'

        when 9 then 'RID(行 ID)'

        when 10 then '应用程序'

    end

    ,对象id=rsc_objid

    ,对象名=b.obj_name

    ,rsc_indid

 from master..syslockinfo a left join #t b on a.req_spid=b.req_spid

go

drop table #t

--2

--try

set nocount on ;

if object_id('T1') is not null

    drop table T1

go

create table T1(ID int primary key,Col1 int,Col2 nvarchar(20))

insert T1 select 1,101,'A'

insert T1 select 2,102,'B'

insert T1 select 3,103,'C'

go

if object_id('T2') is not null

    drop table T2

go

create table T2(ID int primary key,Col1 int,Col2 nvarchar(20))

insert T2 select 1,201,'X'

insert T2 select 2,202,'Y'

insert T2 select 3,203,'Z'

go

生成表數據:

防止死鎖:

1、    最少化阻塞。阻塞越少,發生死鎖機會越少

2、    在事務中按順序訪問表(以上例子:死鎖2)

3、    在錯誤處理程式中檢查錯誤1205並在錯誤發生時重新提交事務

4、    在錯誤處理程式中加一個過程將錯誤的詳細寫入日誌

5、    索引的合理使用(以上例子:死鎖1、死鎖3)

當發生死鎖時,事務自動提交,可通過日誌來監視死鎖

死鎖1(索引):

--連接窗口1

--1步:

begin tran

    update t1 set col2=col2+'A' where col1=101

--3步:

    select * from t2 where col1=201

commit tran

--連接窗口2

--2步:

begin tran

    update t2 set col2=col2+'B' where col1=203

--4步:

    select * from t1 where col1=103

commit tran

--連接窗口1:收到死鎖錯誤,連接窗口2得到結果:

--連接窗口2:得到結果

處理方法:

--在t1、t2表的col1條件列建索引

create index IX_t1_col1 on t1(col1)

create index IX_t2_col1 on t2(col1)

go

--連接窗口1

--1步:

begin tran

    update t1 set col2=col2+'A' where col1=101

--3步:

select * from t2 with(index=IX_t2_col1)where col1=201    --因表數據少,只能指定索引提示才能確保SQL Server使用索引

commit tran

--連接窗口2

--2步:

begin tran

    update t2 set col2=col2+'B' where col1=203

--4步:

select * from t1 with(index=IX_t1_col1) where col1=103    --因表數據少,只能指定索引提示才能確保SQL Server使用索引

commit tran

--連接窗口1:

--連接窗口2

死鎖2(訪問表順序):

--連接窗口1:

--1步:

begin tran

    update t1 set col1=col1+1 where ID=1

--3步:

select col1 from t2 where ID=1

commit tran

--連接窗口2:

--2步:

begin tran

    update t2 set col1=col1+1 where ID=1

--4步

select col1 from t1 where ID=1

commit tran

--連接窗口1:

--連接窗口2:

處理方法:

--改變訪問表的順序

--連接窗口1:

--1步:

begin tran

    update t1 set col1=col1+1 where ID=1

--3步:

    select col1 from t2 where ID=1

commit tran

--連接窗口2:

--2步:

begin tran

    select col1 from t1 where ID=1--會等待連接窗口1提交

--4步

    update t2 set col1=col1+1 where ID=1

commit tran

死鎖3(單表):

--連接窗口1:

while 1=1

    update T1 set col1=203-col1 where ID=2

--連接窗口2:

declare @i  nvarchar(20)

while 1=1

    set @i=(select col2 from T1 with(index=IX_t1_col1)where Col1=102);--因表數據少,只能指定索引提示才能確保SQL Server使用索引

--連接窗口1

處理方法:

1、刪除col1上的非聚集索引,這樣影響SELECT速度,不可取.

    drop index IX_t1_col1 on t1

2、建一個覆蓋索引

    A、drop index IX_t1_col1 on t1

    B、create index IX_t1_col1_col2 on t1(col1,col2)

通過SQL Server Profiler查死鎖信息:

啟動SQL Server Profiler——連接實例——事件選取範圍——顯示所有事件

選擇項:

TSQL——SQL:StmtStarting

Locks——Deadlock graph(這是SQL2005新增事件,生成包含死鎖信息的xml值)

     ——Lock:DeadlockChain 死鎖鏈中的進程產生該事件,可標識死鎖進程的ID並跟蹤操作

     ——Lock:Deadlock 該事件發生了死鎖

--3

1 如何锁一个表的某一行

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

SELECT * FROM table ROWLOCK WHERE id = 1

2 锁定数据库的一个表

SELECT * FROM table WITH (HOLDLOCK)

加锁语句:

sybase:

update 表 set col1=col1 where 1=0 ;

MSSQL:

select col1 from 表 (tablockx) where 1=0 ;

oracle:

LOCK TABLE 表 IN EXCLUSIVE MODE ;

加锁后其它人不可操作,直到加锁用户解锁,用commit或rollback解锁

几个例子帮助大家加深印象

设table1(A,B,C)

A B C

a1 b1 c1

a2 b2 c2

a3 b3 c3

1)排它锁

新建两个连接

在第一个连接中执行以下语句

begin tran

update table1

set A='aa'

where B='b2'

waitfor delay '00:00:30' --等待30秒

commit tran

在第二个连接中执行以下语句

begin tran

select * from table1

where B='b2'

commit tran

若同时执行上述两个语句,则select查询必须等待update执行完毕才能执行即要等待30秒

2)共享锁

在第一个连接中执行以下语句

begin tran

select * from table1 holdlock -holdlock人为加锁

where B='b2'

waitfor delay '00:00:30' --等待30秒

commit tran

在第二个连接中执行以下语句

begin tran

select A,C from table1

where B='b2'

update table1

set A='aa'

where B='b2'

commit tran

若同时执行上述两个语句,则第二个连接中的select查询可以执行

而update必须等待第一个事务释放共享锁转为排它锁后才能执行 即要等待30秒

3)死锁

增设table2(D,E)

D E

d1 e1

d2 e2

在第一个连接中执行以下语句

begin tran

update table1

set A='aa'

where B='b2'

waitfor delay '00:00:30'

update table2

set D='d5'

where E='e1'

commit tran

在第二个连接中执行以下语句

begin tran

update table2

set D='d5'

where E='e1'

waitfor delay '00:00:10'

update table1

set A='aa'

where B='b2'

commit tran

同时执行,系统会检测出死锁,并中止进程

补充一点:

Sql Server2000支持的表级锁定提示

HOLDLOCK 持有共享锁,直到整个事务完成,应该在被锁对象不需要时立即释放,等于SERIALIZABLE事务隔离级别

NOLOCK 语句执行时不发出共享锁,允许脏读 ,等于 READ UNCOMMITTED事务隔离级别

PAGLOCK 在使用一个表锁的地方用多个页锁

READPAST 让sql server跳过任何锁定行,执行事务,适用于READ UNCOMMITTED事务隔离级别只跳过RID锁,不跳过页,区域和表锁

ROWLOCK 强制使用行锁

TABLOCKX 强制使用独占表级锁,这个锁在事务期间阻止任何其他事务使用这个表

UPLOCK 强制在读表时使用更新而不用共享锁

应用程序锁:

应用程序锁就是客户端代码生成的锁,而不是sql server本身生成的锁

处理应用程序锁的两个过程

sp_getapplock 锁定应用程序资源

sp_releaseapplock 为应用程序资源解锁

注意: 锁定数据库的一个表的区别

SELECT * FROM table WITH (HOLDLOCK) 其他事务可以读取表,但不能更新删除

SELECT * FROM table WITH (TABLOCKX) 其他事务不能读取表,更新和删除

SQL code

1 如何锁一个表的某一行

A 连接中执行

SET TRANSACTION ISOLATION LEVEL REPEATABLE READ

begin tran

select * from tablename with (rowlock) where id=3

waitfor delay '00:00:05'

commit tran

B连接中如果执行

update tablename set colname='10' where id=3 --则要等待5秒

update tablename set colname='10' where id<>3 --可立即执行

2 锁定数据库的一个表

SELECT * FROM table WITH (HOLDLOCK)

注意: 锁定数据库的一个表的区别

SELECT * FROM table WITH (HOLDLOCK)

其他事务可以读取表,但不能更新删除

SELECT * FROM table WITH (TABLOCKX)

其他事务不能读取表,更新和删除

--4

--处理死锁

 查看当前进程,或死锁进程,并能自动杀掉死进程

 因为是针对死的,所以如果有死锁进程,只能查看死锁进程

 当然,你可以通过参数控制,不管有没有死锁,都只查看死锁进程

--邹建 2004.4--

--调用示例

 exec p_lockinfo

--

create proc p_lockinfo

@kill_lock_spid bit=1,  --是否杀掉死锁的进程,1 杀掉, 0 仅显示

@show_spid_if_nolock bit=1 --如果没有死锁的进程,是否显示正常进程信息,1 显示,0 不显示

as

declare @count int,@s nvarchar(1000),@i int

select id=identity(int,1,1),标志,

 进程ID=spid,线程ID=kpid,块进程ID=blocked,数据库ID=dbid,

 数据库名=db_name(dbid),用户ID=uid,用户名=loginame,累计CPU时间=cpu,

 登陆时间=login_time,打开事务数=open_tran, 进程状态=status,

 工作站名=hostname,应用程序名=program_name,工作站进程ID=hostprocess,

 域名=nt_domain,网卡地址=net_address

into #t from(

 select 标志='死锁的进程',

  spid,kpid,a.blocked,dbid,uid,loginame,cpu,login_time,open_tran,

  status,hostname,program_name,hostprocess,nt_domain,net_address,

  s1=a.spid,s2=0

 from master..sysprocesses a join (

  select blocked from master..sysprocesses group by blocked

  )b on a.spid=b.blocked where a.blocked=0

 union all

 select '_牺牲品_',

  spid,kpid,blocked,dbid,uid,loginame,cpu,login_time,open_tran,

  status,hostname,program_name,hostprocess,nt_domain,net_address,

  s1=blocked,s2=1

 from master..sysprocesses a where blocked0

)a order by s1,s2

select @[email protected]@rowcount,@i=1

if @count=0 and @show_spid_if_nolock=1

begin

 insert #t

 select 标志='正常的进程',

  spid,kpid,blocked,dbid,db_name(dbid),uid,loginame,cpu,login_time,

  open_tran,status,hostname,program_name,hostprocess,nt_domain,net_address

 from master..sysprocesses

 set @[email protected]@rowcount

end

if @count0

begin

 create table #t1(id int identity(1,1),a nvarchar(30),b Int,EventInfo nvarchar(255))

 if @kill_lock_spid=1

 begin

  declare @spid varchar(10),@标志 varchar(10)

  while @[email protected]

  begin

   select @spid=进程ID,@标志=标志 from #t where [email protected]

   insert #t1 exec('dbcc inputbuffer('[email protected]+')')

   if @标志='死锁的进程' exec('kill '[email protected])

   set @[email protected]+1

  end

 end

 else

  while @[email protected]

  begin

   select @s='dbcc inputbuffer('+cast(进程ID as varchar)+')' from #t where [email protected]

   insert #t1 exec(@s)

   set @[email protected]+1

  end

 select a.,进程的SQL语句=b.EventInfo

 from #t a join #t1 b on a.id=b.id

end

go

--5

 SQL Server锁类型(SQL) 收藏

1. HOLDLOCK 在该表上保持共享锁,直到整个事务结束,而不是在语句执行完立即释放所添加的锁。  

  2. NOLOCK:不添加共享锁和排它锁,当这个选项生效后,可能读到未提交读的数据或“脏数据”,这个选项仅仅应用于SELECT语句。  

  3. PAGLOCK:指定添加页锁(否则通常可能添加表锁)。 

  4. READCOMMITTED用与运行在提交读隔离级别的事务相同的锁语义执行扫描。默认情况下,SQL Server 2000 在此隔离级别上操作。。 

  5. READPAST 跳过已经加锁的数据行,这个选项将使事务读取数据时跳过那些已经被其他事务锁定的数据行,而不是阻塞直到其他事务释放锁,READPAST仅仅应用于READ COMMITTED隔离性级别下事务操作中的SELECT语句操作。  

  6. READUNCOMMITTED:等同于NOLOCK。  

  7. REPEATABLEREAD:设置事务为可重复读隔离性级别。 

  8. ROWLOCK:使用行级锁,而不使用粒度更粗的页级锁和表级锁。  

  9. SERIALIZABLE:用与运行在可串行读隔离级别的事务相同的锁语义执行扫描。等同于 HOLDLOCK。 

  10. TABLOCK:指定使用表级锁,而不是使用行级或页面级的锁,SQL Server在该语句执行完后释放这个锁,而如果同时指定了HOLDLOCK,该锁一直保持到这个事务结束。  

  11. TABLOCKX:指定在表上使用排它锁,这个锁可以阻止其他事务读或更新这个表的数据,直到这个语句或整个事务结束。 

  12. UPDLOCK :指定在读表中数据时设置更新 锁(update lock)而不是设置共享锁,该锁一直保持到这个语句或整个事务结束,使用UPDLOCK的作用是允许用户先读取数据(而且不阻塞其他用户读数据),并且保证在后来再更新数据时,这一段时间内这些数据没有被其他用户修改。

本文来自CSDN博客,转载请标明出处:httpblog.csdn.netarrow_gxarchive200806012501368.aspx

--SQL Server 2005中解决死锁问题

数据库操作的死锁是不可避免的,本文并不打算讨论死锁如何产生,重点在于解决死锁,通过SQL Server 2005, 现在似乎有了一种新的解决办法。 

将下面的SQL语句放在两个不同的连接里面,并且在5秒内同时执行,将会发生死锁。 

use Northwind

begin tran

  insert into Orders(CustomerId) values(@#[email protected]#)

  waitfor delay @#[email protected]#

  select  from Orders where CustomerId = @#[email protected]#

commit

print @#end [email protected]# 

   

SQL Server对付死锁的办法是牺牲掉其中的一个,抛出异常,并且回滚事务。在SQL Server 2000,语句一旦发生异常,T-SQL将不会继续运行,上面被牺牲的连接中, print @#end [email protected]#语句将不会被运行,所以我们很难在SQL Server 2000的T-SQL中对死锁进行进一步的处理。 

现在不同了,SQL Server 2005可以在T-SQL中对异常进行捕获,这样就给我们提供了一条处理死锁的途径: 

下面利用的try ... catch来解决死锁。 

SET XACT_ABORT ON

declare @r int

set @r = 1

while @r  = 3

begin

  begin tran

  

  begin try   

    insert into Orders(CustomerId) values(@#[email protected]#)

    waitfor delay @#[email protected]#

    select  from Orders where CustomerId = @#[email protected]#

    

    commit

    break

  end try

    

  begin catch

    rollback

    waitfor delay @#[email protected]#

    set @r = @r + 1

    continue

  end catch

end 

   

解决方法当然就是重试,但捕获错误是前提。rollback后面的waitfor不

解决方法当然就是重试,但捕获错误是前提。rollback后面的waitfor不可少,发生冲突后需要等待一段时间,@retry数目可以调整以应付不同的要求。 

但是现在又面临一个新的问题 错误被掩盖了,一但问题发生并且超过3次,异常却不会被抛出。SQL Server 2005 有一个RaiseError语句,可以抛出异常,但却不能直接抛出原来的异常,所以需要重新定义发生的错误,现在,解决方案变成了这样 

declare @r int

set @r = 1

while @r  = 3

begin

  begin tran

  

  begin try   

    insert into Orders(CustomerId) values(@#[email protected]#)

    waitfor delay @#[email protected]#

    select  from Orders where CustomerId = @#[email protected]#

    

    commit

    break

  end try

    

  begin catch

    rollback

    waitfor delay @#[email protected]#

    set @r = @r + 1

    continue

  end catch

end

if ERROR_NUMBER()   0

begin

  declare @ErrorMessage nvarchar(4000);

  declare @ErrorSeverity int;

  declare @ErrorState int;

  select

    @ErrorMessage = ERROR_MESSAGE(),

    @ErrorSeverity = ERROR_SEVERITY(),

    @ErrorState = ERROR_STATE();

  raiserror (@ErrorMessage,

        @ErrorSeverity,

        @ErrorState

        );

end 

   

我希望将来SQL Server 2005能够直接抛出原有异常,比如提供一个无参数的RaiseError。 

因此方案有点臃肿,但将死锁问题封装到T-SQL中有助于明确职责,提高高层系统的清晰度。现在,对于DataAccess的代码,或许再也不需要考虑死锁问题了。