Skip to content

SQL-472: Types of Locks in SQL Server #360

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 138 additions & 0 deletions sql-constraints/sql-locks/sqlserver-locks.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
-- Change schema to University
USE University;

--Shared Lock (S)

--Session 1
-- Set the isolation level to REPEATABLE READ to hold shared locks until commit/rollback
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;

BEGIN TRAN;
SELECT *
FROM Course
WHERE id='CS111';

--Check the locks held by this session:
SELECT request_session_id, resource_type, resource_description, request_mode, resource_associated_entity_id
FROM sys.dm_tran_locks
WHERE request_session_id = @@SPID;

--Commit later
COMMIT TRAN;

--Session 2
SELECT *
FROM Course
WHERE id='CS111';

BEGIN TRAN;

UPDATE Course
SET credits = 8
WHERE id='CS111';

ROLLBACK TRAN;

--Sesion 3
SELECT
session_id,
blocking_session_id,
command,
status,
wait_type,
wait_time,
last_wait_type,
open_transaction_count,
text
FROM sys.dm_exec_requests
CROSS APPLY sys.dm_exec_sql_text(sql_handle)
WHERE blocking_session_id <> 0;

--EXEC sp_who2;

--Exclusive Lock (X)

--Session 1

-- Set the isolation level to REPEATABLE READ to hold shared locks until commit/rollback
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;

BEGIN TRAN;
UPDATE Course
SET credits = 8
WHERE id='CS111';

--Check the locks held by this session:
SELECT request_session_id, resource_type, resource_description, request_mode, resource_associated_entity_id
FROM sys.dm_tran_locks
WHERE request_session_id = @@SPID;

--Rollback later
ROLLBACK TRAN;

--Session 2
BEGIN TRAN;
SELECT *
FROM Course
WHERE id='CS111';

COMMIT TRAN;


--Update Lock (U)

--Session 1
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;

BEGIN TRAN;
SELECT *
FROM Course
WITH (UPDLOCK)
WHERE id='CS111';

--Check the locks held by this session:
SELECT request_session_id, resource_type, resource_description, request_mode
FROM sys.dm_tran_locks
WHERE request_session_id = @@SPID;

--Session 2
SELECT *
FROM Course
WHERE id='CS111';

UPDATE Course
SET credits = 8
WHERE id='CS111';

--Shared with Intent Exclusive (SIX)

--Session 1
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;

BEGIN TRAN;

-- Explicitly request a SIX lock:
SELECT *
FROM Course
WITH (TABLOCKX, HOLDLOCK)
WHERE id='CS111';

UPDATE Course
SET credits = 8
WHERE id='CS111';

--Rollback later
ROLLBACK TRAN;

SELECT request_session_id, resource_type, resource_description, request_mode
FROM sys.dm_tran_locks
WHERE request_session_id = @@SPID;

--Session 2
BEGIN TRAN;

SELECT *
FROM Course
WHERE id='CS111';

COMMIT TRAN;