SQL SERVER 锁定的实例

---实例DB:AdventureWorks2014

--- 创建view DBLocks

USE [AdventureWorks2014]
GO

/****** Object:  View [dbo].[DBlocks]    Script Date: 8/17/2016 6:38:16 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE VIEW [dbo].[DBlocks] AS
SELECT request_session_id as spid,
db_name(resource_database_id) as dbname,
CASE
WHEN 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)
END as 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 LEFT JOIN sys.partitions p
ON p.partition_id = t.resource_associated_entity_id
WHERE resource_database_id = db_id();
GO
View Code

实例1: Read Commit(SQL SERVER默认隔离级别) 隔离级别下的SELECT

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
BEGIN TRAN
SELECT * FROM [Production].[Product] WHERE Name ='Reflector';
SELECT * FROM [dbo].[DBlocks] WHERE spid= @@SPID
COMMIT TRAN
View Code

DBLocks 的输出结果

spiddbnameentity_nameindex_idresourcemodestatus
58AdventureWorks2014sysrowsetsNULLOBJECTSch-SGRANT
58AdventureWorks2014n/aNULLDATABASESGRANT
58AdventureWorks2014DBlocksNULLOBJECTISGRANT

 Product 表的数据没有LOCK,所以批处理执行select 操作获得了共享锁。SQL SEVER一旦读完数据之后立即释放共享锁。因此执行DBLoks试图时,上面提到的共享锁不存在了。

实例2: 可重复读隔离级别下的SELECT

SQL 批处理

 

SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
BEGIN TRAN
SELECT * FROM [Production].[Product] WHERE Name ='Reflector';
SELECT * FROM [dbo].[DBlocks] WHERE spid= @@SPID
COMMIT TRAN
View Code

DBLocks 的输出结果

spiddbnameentity_nameindex_idresourcedescriptionmodestatus
58AdventureWorks2014sysrowsetsNULLOBJECT Sch-SGRANT
58AdventureWorks2014n/aNULLDATABASE SGRANT
58AdventureWorks2014Product1PAGE0.590972222ISGRANT
58AdventureWorks2014DBlocksNULLOBJECT ISGRANT
58AdventureWorks2014Product1KEY(b031e2166063)SGRANT
58AdventureWorks2014Product3KEY(489415cc61a4)SGRANT
58AdventureWorks2014ProductNULLOBJECT ISGRANT
58AdventureWorks2014Product3PAGE3.626388889ISGRANT

由于Product表上有聚集索引,数据行就是页级别的全部索引行,返回两个数据行上的锁是键锁,用来寻找相关数据。在Product表上存在Name上的非聚集索引,可以通过Index_ID字段的值来区分聚集所以和非聚集索引,Index_ID=1表示数据行和聚集索引,Index_ID=3表示非聚集索引。由于事务的隔离级别是可重复读,共享锁要等到事务结束为止。NOTE:索引行拥有共享锁(S)而数据分页,索引分页以及表本身拥有意向共享锁(IS)。

实例3:可串行隔离级别下的SELECT

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
BEGIN TRAN
SELECT * FROM [Production].[Product] WHERE Name LIKE 'Reflect%';
SELECT * FROM [dbo].[DBlocks] WHERE spid= @@SPID
COMMIT TRAN
View Code

DBLocks的输出结果

 

spiddbnameentity_nameindex_idresourcedescriptionmodestatus
58AdventureWorks2014sysrowsetsNULLOBJECT Sch-SGRANT
58AdventureWorks2014n/aNULLDATABASE SGRANT
58AdventureWorks2014Product1PAGE0.590972222ISGRANT
58AdventureWorks2014n/aNULLMETADATAschema_id = 7Sch-SGRANT
58AdventureWorks2014DBlocksNULLOBJECT ISGRANT
58AdventureWorks2014Product1KEY(b031e2166063)SGRANT
58AdventureWorks2014Product3KEY(489415cc61a4)RangeS-SGRANT
58AdventureWorks2014ProductNULLOBJECT ISGRANT
58AdventureWorks2014Product3PAGE3.626388889ISGRANT
58AdventureWorks2014Product3KEY(53bdd09497c4)RangeS-SGRANT
58AdventureWorks2014NULLNULLOBJECT ISGRANT

转载于:https://www.cnblogs.com/Jesse-Li/p/5783692.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值