看清楚 select * from authorswith (xlock) where au_id='172-32-1176',使用的是“表提示”手动为记录加锁。
不知道楼主的用在什么地方,我的建议是使用 update 锁,串行化数据的修改,可以避免私锁问题。
[/Quote]
用with (xlock)只是加了IX锁(意向排它锁),所以不会阻塞其它事务的SELECT,而用with(xlock,paglock)则加了X锁(排它锁),这样阻塞其它事务。看来READ COMMITTED隔离级别不可以锁住单行?
begin tran
select * from authors with (xlock) where au_id='172-32-1176'
--rollback tran
sp_lock:
spid dbid ObjId IndId Type Resource Mode Status
------ ------ ----------- ------ ---- ---------------- -------- ------
57 5 0 0 DB S GRANT
58 5 1977058079 1 PAG 1:197 IX GRANT
58 5 0 0 DB S GRANT
58 5 1977058079 1 KEY (02014f0bec4e) X GRANT
58 1 85575343 0 TAB IS GRANT
58 5 1977058079 0 TAB IX GRANT
59 5 0 0 DB S GRANT
begin tran
select * from authors with (xlock,paglock) where au_id='172-32-1176'
sp_lock:
spid dbid ObjId IndId Type Resource Mode Status
------ ------ ----------- ------ ---- ---------------- -------- ------
57 5 0 0 DB S GRANT
58 5 1977058079 1 PAG 1:197 X GRANT
58 5 0 0 DB S GRANT
58 1 85575343 0 TAB IS GRANT
58 5 1977058079 0 TAB IX GRANT
59 5 0 0 DB S GRANT
通过“事件跟踪器”的跟踪。
第一次执行 select * from pubs..authors where au_id='172-32-1176' 语句时只获取页面的意向锁。
而执行 update pubs..authors set address='Ad. '+address where au_id='172-32-1176' 语句后,再执行 select * from pubs..authors where au_id='172-32-1176' 语句会获取索引码的共享锁。
这可能与 SQL SERVER 的查询优化方案有关。