CREATEVIEW DBlocks AS SELECT request_session_id as spid, db_name(resource_database_id) as dbname, CASEWHEN resource_type='OBJECT' THEN object_name(resource_associated_entity_id) WHEN resource_associated_entity_id=0 THEN'n/a' ELSE object_name(p.object_id) ENDas entity_name, index_id,resource_type as resource, resource_description as description, request_mode as mode, request_status as status FROM sys.dm_tran_locks t LEFTJOIN sys.partitions p ON p.hobt_id = t.resource_associated_entity_id WHERE resource_database_id=db_id()
repeatable read
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
USE student
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ
BEGIN TRAN
SELECT* FROM S WHERE AGE>20
/* 查询当前进程的锁 */ SELECT* FROM Dblocks WHERE spid=@@spid AND entity_name='S'
COMMIT TRAN
read committed
read committed 是短锁,执行完就释放了
1 2 3 4 5 6 7 8 9 10 11 12 13
USE student
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
BEGIN TRAN
SELECT* FROM S WHERE AGE>20
EXEC sp_lock@@spid
COMMIT TRAN
只有数据库上有一个读锁
spid
Database
ObjId
IndId
Type
Resource
Mode
Status
51
student
0
0
DB
S
GRANT
repeatabel read
长锁
1 2 3 4 5 6 7 8 9 10 11 12 13
USE student
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ
BEGIN TRAN
SELECT* FROM S WHERE AGE>20
EXEC sp_lock @@spid
COMMIT TRAN
自己加读锁,祖先节点加意向读锁
聚簇索引,主码上有索引,KEY 来标一条记录
spid
Database
ObjId
IndId
Type
Resource
Mode
Status
51
student
0
0
DB
S
GRANT
51
student
2025058250
2
PAG
1:31
IS
GRANT
51
student
2025058250
1
PAG
1:30
IS
GRANT
51
student
2025058250
1
KEY
(930085dfee46)
S
GRANT
51
student
2025058250
0
TAB
IS
GRANT
51
student
2025058250
2
KEY
(a8007c139223)
S
GRANT
51
student
2025058250
2
KEY
(ab008cdbe580)
S
GRANT
51
student
2025058250
1
KEY
(930022f0d614)
S
GRANT
serializable
1 2 3 4 5 6 7 8 9 10 11 12 13
USE student
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
BEGIN TRAN
SELECT* FROM S WHERE AGE>20
EXEC sp_lock@@spid
COMMIT TRAN
为了防止幻象,得加码范围锁
spid
Database
ObjId
IndId
Type
Resource
Mode
Status
51
student
0
0
DB
S
GRANT
51
student
2025058250
2
PAG
1:31
IS
GRANT
51
student
2025058250
1
PAG
1:30
IS
GRANT
51
student
2025058250
1
KEY
(930085dfee46)
S
GRANT
51
student
2025058250
0
TAB
IS
GRANT
51
student
2025058250
2
KEY
(a8007c139223)
RangeS-S
GRANT
51
student
2025058250
2
KEY
(ab008cdbe580)
RangeS-S
GRANT
51
student
2025058250
2
KEY
(ffffffffffff)
RangeS-S
GRANT
51
student
2025058250
1
KEY
(930022f0d614)
S
GRANT
SQL Server 更新
read committed
1 2 3 4 5 6 7 8 9 10 11 12
USE student
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
BEGIN TRAN
UPDATE S SET SEX=0 WHERE AGE>20 EXEC sp_lock@@spid
ROLLBACK TRAN
spid
Database
ObjId
IndId
Type
Resource
Mode
Status
52
student
0
0
DB
S
GRANT
52
student
2025058250
1
PAG
1:30
IX
GRANT
52
student
2025058250
1
KEY
(930085dfee46)
X
GRANT
52
student
2025058250
0
TAB
IX
GRANT
52
student
2025058250
1
KEY
(930022f0d614)
X
GRANT
SQL Server 创建表
read committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
USE student
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
BEGIN TRAN
SELECT* INTO OldS FROM S WHERE AGE>20
EXEC sp_lock2@@spid
COMMIT TRAN
创建表加写锁
Sch-M 模式修改锁,避免在建表的时候其他事务访问它
spid
Database
ObjId
IndId
Type
Resource
Mode
Status
54
student
0
0
EXT
1:112
X
GRANT
54
student
0
0
PAG
1:127
X
GRANT
54
student
0
0
PAG
1:124
X
GRANT
54
student
0
0
PAG
1:125
X
GRANT
54
student
0
0
PAG
1:122
X
GRANT
54
student
0
0
PAG
1:123
X
GRANT
54
student
0
0
PAG
1:120
X
GRANT
54
student
0
0
PAG
1:121
X
GRANT
54
student
2041058307
0
TAB
Sch-M
GRANT
SQL Server 行锁
read committed
1 2 3 4 5 6 7 8 9 10 11 12
USE student
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
BEGIN TRAN UPDATE OldS SET SNAME='王非' WHERE S# ='s1'