SlideShare a Scribd company logo
1 of 9
IF EXISTS( SELECT 1
FROM sys.views
WHERE name = 'DBlocks')
DROP VIEWDBlocks;
GO
CREATE VIEW DBlocksAS
SELECT request_session_idASspid,
DB_NAME(resource_database_id) ASdbname ,
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)
END ASentity_name ,
index_id,
resource_type ASresource ,
resource_descriptionASdescription,
request_modeASmode ,
request_statusASstatus
FROM sys.dm_tran_lockst
LEFT JOIN sys.partitionsp
ON p.partition_id=t.resource_associated_entity_id
WHERE resource_database_id=DB_ID()
ANDresource_type <>'DATABASE' ;
GO
-- Example 1: SELECT withREAD COMMITTED
-- isolationlevel
USE General;
SET TRANSACTION ISOLATION LEVELREADCOMMITTED ;
BEGIN TRAN
SELECT *
FROM dbo.GenderNG
WHERE gendername ='Reflector';
SELECT *
FROM DBlocks
WHERE spid= @@spid;
COMMIT TRAN
/*There are nolockson the data in the Production.Producttable because the batch
was doingonlySELECT operations,andsoacquiredonlySlocks.By default,SQLServer
releasesSlocksassoon as ithas finishedreadingthe dataso,by the time we execute
the SELECT fromthe view,SQLServerno longerholdsthe locks.
*/
-- Example 2: SELECT withREPEATABLE READ
-- isolationlevel
USE General ;
SET TRANSACTION ISOLATION LEVELREPEATABLEREAD ;
BEGIN TRAN
SELECT gendername,sex
FROM dbo.GenderNG
WHERE gendername LIKE'Ana%';
SELECT *
FROM DBlocks
WHERE spid= @@spid
ANDentity_name ='GenderNG';
COMMIT TRAN
/*
Thistime,because the transactionisolationlevelisREPEATABLEREAD,SQL Serverholds
the S locksuntil the transactionisfinishedandsowe can see theminour results.The
dbo.GenderNGtable hasaclusteredindex,sothe rowsof data are all index rows
inthe leaf level.Assuch,the locksonthe 19 individual datarows
returnedare KEY locks.The table alsohas a non-clusteredindexonthe genderNamecolumn
and we can see 19 KEY locksat the leaf level of thisnon-clusteredindex,usedtofindthe
relevantrows.
*/
-- Example 3: SELECT withSERIALIZABLEisolation
-- level
USE General ;
SET TRANSACTION ISOLATION LEVELSERIALIZABLE;
BEGIN TRAN
SELECT *
FROM dbo.GenderNG
WHERE gendername LIKE'Ana%';
SELECT *
FROM DBlocks
WHERE spid= @@spid
ANDentity_name ='GenderNG';
COMMIT TRAN
/*
The two-partmode RangeS-Sindicatesakey-range lockinadditiontothe lockon the
keyitself.The firstpart(RangeS) isthe lockonthe range of keysbetweenandincluding
the keyholdingthe lockandthe previouskeyinthe index.The key-rangelocksprevent
othertransactionsfrominsertinganynew rowsintothe table thatmeetthe conditionof
thisquery;that is,it'snot possible toinsertanynew rowswithaproduct name starting
withRacingSocks.The key-range locksare heldonrangesinthe non-clusteredindex
on gendername (Index_id=3) because thatisthe index usedto findthe qualifyingrows.
*/
-- Example 4: Update withREAD COMMITTED
-- isolationlevel
USE General ;
SET TRANSACTION ISOLATION LEVELREADCOMMITTED ;
BEGIN TRAN
UPDATE dbo.GenderNG
SET Sex = 1
WHERE gendername LIKE'Adam%';
SELECT *
FROM DBlocks
WHERE spid= @@spid
ANDentity_name ='GenderNG';
COMMIT TRAN
/*
the two rowsinthe leaf level of the clusteredindex are lockedwith
X locks.The page and the table are thenlockedwithIXlocks.
As discussedearlier,SQLServeracquiresUlockswhile itlooksforthe rowsto update.
However,SQLServerescalatesthese toXlocksuponperformingthe actual update and,
by the time we lookat the DBlocksview,the U locksare gone.Unlesswe force U locks
witha queryhint,we mightneversee theminthe lockreportfromDBlocks,or by direct
inspectionof sys.dm_tran_locks.
*/
-- Example 5: Update withSERIALIZABLEisolation
-- level (withanindex)
USE General ;
SET TRANSACTION ISOLATION LEVELSERIALIZABLE
BEGIN TRAN
UPDATE dbo.GenderNG
SET Sex = 1
WHERE gendername LIKE'Adam%';
SELECT *
FROM DBlocks
WHERE spid= @@spid
ANDentity_name ='GenderNG';
COMMIT TRAN
/*
Again,notice thatthe key-range locksare onthe non-clusteredindex,usedtofindthe
relevantrows.The range interval itself needsonlyanSlockto preventinsertions,but
the searchedkeyshave U locks,ensuringthatnootherprocesscan attempttoUPDATE
them.The keysinthe table itself (index_id= 1) obtainthe X lockwhenthe actual
modificationismade.
*/
-- Example 6: Update withSERIALIZABLEisolation
-- level notusinganindex
USE General ;
SET TRANSACTION ISOLATION LEVELSERIALIZABLE
BEGIN TRAN
UPDATE dbo.GenderNG
SET Sex = 1
WHERE Sex = 2 ;
SELECT *
FROM DBlocks
WHERE spid= @@spid
ANDentity_name ='GenderNG';
COMMIT TRAN
/*
As there wasno useful index,aclusteredindexscanonthe entire table wasrequired,
and so all keysinitiallyreceivedthe RangeS-Ulock;whenfourrowswere eventually
modified,the locksonthose keysescalatedtothe RangeX-Xlock.We cansee twoof
the RangeX-Xlocks,anda fewof the RangeS-Ulocks.The complete outputhas501
RangeS-Ulocks,as well asIU lockson several pages,IXlocksontwopages,and an IX
lockon the table.
*/
-- Example 7: Creatinga table
USE General ;
SET TRANSACTION ISOLATION LEVELREADCOMMITTED ;
BEGIN TRAN
SELECT *
INTO newGender
FROM dbo.GenderNG
WHERE Sex BETWEEN 1 AND 2 ;
SELECT *
FROM DBlocks
WHERE spid= @@spid;
COMMIT TRAN
/*
SQL Serveracquiredveryfewof these locksonelementsof the
newGendertable.Inthe entity_name column,note thatmostof the objectsare
undocumented, andnormallyinvisible,systemtable names.Whencreatingthe new table,
SQL Serveracquireslocksonsix differentsystemtablestorecordinformationabout
thisnewtable.Inaddition,notice the schemamodification(Sch-M) locks
*/
-- Example 8: RID locks
USE General ;
SET TRANSACTION ISOLATION LEVELREADCOMMITTED
BEGIN TRAN
UPDATE newGender
SET Sex = 1
WHERE genderName ='Alan';
SELECT *
FROM DBlocks
WHERE spid= @@spid
ANDentity_name ='newGender';
COMMIT TRAN
/*
There are noindexesonthe newProductstable,sothe lockonthe actual row meeting
our criterionisan Xlock on the row(RID).For RID locks,the descriptionactuallyreports
the specificrowinthe form File Number:Page number:Slotnumber.Asexpected,SQL
ServertakesIXlockson the page and the table.
*/

More Related Content

What's hot

Func dyn title_set.c
Func dyn title_set.cFunc dyn title_set.c
Func dyn title_set.c
albertinous
 
Oracle常用经典Sql查询
Oracle常用经典Sql查询Oracle常用经典Sql查询
Oracle常用经典Sql查询
yiditushe
 

What's hot (18)

Database decommission process
Database decommission processDatabase decommission process
Database decommission process
 
Drupal7 dbtng
Drupal7  dbtngDrupal7  dbtng
Drupal7 dbtng
 
MySql:Introduction
MySql:IntroductionMySql:Introduction
MySql:Introduction
 
Testing orm based code
Testing orm based codeTesting orm based code
Testing orm based code
 
Oracle ORA Errors
Oracle ORA ErrorsOracle ORA Errors
Oracle ORA Errors
 
Database administration commands
Database administration commands Database administration commands
Database administration commands
 
Func dyn title_set.c
Func dyn title_set.cFunc dyn title_set.c
Func dyn title_set.c
 
Discuss the scrollable result set in jdbc
Discuss the scrollable result set in jdbcDiscuss the scrollable result set in jdbc
Discuss the scrollable result set in jdbc
 
PostgreSQL's Secret NoSQL Superpowers
PostgreSQL's Secret NoSQL SuperpowersPostgreSQL's Secret NoSQL Superpowers
PostgreSQL's Secret NoSQL Superpowers
 
Memory Management
Memory ManagementMemory Management
Memory Management
 
Oracle常用经典Sql查询
Oracle常用经典Sql查询Oracle常用经典Sql查询
Oracle常用经典Sql查询
 
Lazy evaluation drupal camp moscow 2014
Lazy evaluation drupal camp moscow 2014Lazy evaluation drupal camp moscow 2014
Lazy evaluation drupal camp moscow 2014
 
SparkSQLの構文解析
SparkSQLの構文解析SparkSQLの構文解析
SparkSQLの構文解析
 
Php verses MySQL
Php verses MySQLPhp verses MySQL
Php verses MySQL
 
Beginner guide to mysql command line
Beginner guide to mysql command lineBeginner guide to mysql command line
Beginner guide to mysql command line
 
Mysql DBI
Mysql DBIMysql DBI
Mysql DBI
 
15 MySQL Basics #burningkeyboards
15 MySQL Basics #burningkeyboards15 MySQL Basics #burningkeyboards
15 MySQL Basics #burningkeyboards
 
Basedatos2
Basedatos2Basedatos2
Basedatos2
 

Similar to Lock basicsexamples

ASP.Net Presentation Part2
ASP.Net Presentation Part2ASP.Net Presentation Part2
ASP.Net Presentation Part2
Neeraj Mathur
 
SSMS-waitstats
SSMS-waitstatsSSMS-waitstats
SSMS-waitstats
E Blake
 

Similar to Lock basicsexamples (20)

Drupal II: The SQL
Drupal II: The SQLDrupal II: The SQL
Drupal II: The SQL
 
Inexpensive Datamasking for MySQL with ProxySQL — Data Anonymization for Deve...
Inexpensive Datamasking for MySQL with ProxySQL — Data Anonymization for Deve...Inexpensive Datamasking for MySQL with ProxySQL — Data Anonymization for Deve...
Inexpensive Datamasking for MySQL with ProxySQL — Data Anonymization for Deve...
 
JDBC (2).ppt
JDBC (2).pptJDBC (2).ppt
JDBC (2).ppt
 
ASP.Net Presentation Part2
ASP.Net Presentation Part2ASP.Net Presentation Part2
ASP.Net Presentation Part2
 
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
 
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
 
PostgreSQL Terminology
PostgreSQL TerminologyPostgreSQL Terminology
PostgreSQL Terminology
 
Oracle notes
Oracle notesOracle notes
Oracle notes
 
Sql
SqlSql
Sql
 
SSMS-waitstats
SSMS-waitstatsSSMS-waitstats
SSMS-waitstats
 
MYSQL - PHP Database Connectivity
MYSQL - PHP Database ConnectivityMYSQL - PHP Database Connectivity
MYSQL - PHP Database Connectivity
 
Ace Up the Sleeve
Ace Up the SleeveAce Up the Sleeve
Ace Up the Sleeve
 
.Net Project Portfolio for Roger Loving
.Net Project Portfolio for Roger Loving.Net Project Portfolio for Roger Loving
.Net Project Portfolio for Roger Loving
 
Jdbc ja
Jdbc jaJdbc ja
Jdbc ja
 
MySQL as a Document Store
MySQL as a Document StoreMySQL as a Document Store
MySQL as a Document Store
 
Postgresql
PostgresqlPostgresql
Postgresql
 
CRESTCon Asia 2018 - Config Password Encryption Gone Wrong
CRESTCon Asia 2018 - Config Password Encryption Gone WrongCRESTCon Asia 2018 - Config Password Encryption Gone Wrong
CRESTCon Asia 2018 - Config Password Encryption Gone Wrong
 
Jdbc
JdbcJdbc
Jdbc
 
oracle dba
oracle dbaoracle dba
oracle dba
 
Dropping unique constraints in sql server
Dropping unique constraints in sql serverDropping unique constraints in sql server
Dropping unique constraints in sql server
 

More from Mahabubur Rahaman (6)

Transaction isolationexamples
Transaction isolationexamplesTransaction isolationexamples
Transaction isolationexamples
 
Sql server concurrency
Sql server concurrencySql server concurrency
Sql server concurrency
 
supporting t-sql scripts for IndexPage, Datapage and IndexDefragmentation
supporting t-sql scripts for IndexPage, Datapage and IndexDefragmentationsupporting t-sql scripts for IndexPage, Datapage and IndexDefragmentation
supporting t-sql scripts for IndexPage, Datapage and IndexDefragmentation
 
supporting t-sql scripts for Heap vs clustered table
supporting t-sql scripts for Heap vs clustered tablesupporting t-sql scripts for Heap vs clustered table
supporting t-sql scripts for Heap vs clustered table
 
Introduction of sql server indexing
Introduction of sql server indexingIntroduction of sql server indexing
Introduction of sql server indexing
 
Introduction to Apache Hadoop Ecosystem
Introduction to Apache Hadoop EcosystemIntroduction to Apache Hadoop Ecosystem
Introduction to Apache Hadoop Ecosystem
 

Recently uploaded

➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men 🔝malwa🔝 Escorts Ser...
➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men  🔝malwa🔝   Escorts Ser...➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men  🔝malwa🔝   Escorts Ser...
➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men 🔝malwa🔝 Escorts Ser...
amitlee9823
 
Call Girls In Nandini Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Nandini Layout ☎ 7737669865 🥵 Book Your One night StandCall Girls In Nandini Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Nandini Layout ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 
CHEAP Call Girls in Rabindra Nagar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Rabindra Nagar  (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Rabindra Nagar  (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Rabindra Nagar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night StandCall Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 
➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men 🔝mahisagar🔝 Esc...
➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men  🔝mahisagar🔝   Esc...➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men  🔝mahisagar🔝   Esc...
➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men 🔝mahisagar🔝 Esc...
amitlee9823
 
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
amitlee9823
 
➥🔝 7737669865 🔝▻ Mathura Call-girls in Women Seeking Men 🔝Mathura🔝 Escorts...
➥🔝 7737669865 🔝▻ Mathura Call-girls in Women Seeking Men  🔝Mathura🔝   Escorts...➥🔝 7737669865 🔝▻ Mathura Call-girls in Women Seeking Men  🔝Mathura🔝   Escorts...
➥🔝 7737669865 🔝▻ Mathura Call-girls in Women Seeking Men 🔝Mathura🔝 Escorts...
amitlee9823
 
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get CytotecAbortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Riyadh +966572737505 get cytotec
 
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
amitlee9823
 
Just Call Vip call girls Bellary Escorts ☎️9352988975 Two shot with one girl ...
Just Call Vip call girls Bellary Escorts ☎️9352988975 Two shot with one girl ...Just Call Vip call girls Bellary Escorts ☎️9352988975 Two shot with one girl ...
Just Call Vip call girls Bellary Escorts ☎️9352988975 Two shot with one girl ...
gajnagarg
 
Call Girls In Shivaji Nagar ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Shivaji Nagar ☎ 7737669865 🥵 Book Your One night StandCall Girls In Shivaji Nagar ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Shivaji Nagar ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
amitlee9823
 
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
amitlee9823
 
Just Call Vip call girls kakinada Escorts ☎️9352988975 Two shot with one girl...
Just Call Vip call girls kakinada Escorts ☎️9352988975 Two shot with one girl...Just Call Vip call girls kakinada Escorts ☎️9352988975 Two shot with one girl...
Just Call Vip call girls kakinada Escorts ☎️9352988975 Two shot with one girl...
gajnagarg
 
Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...
Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...
Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...
gajnagarg
 

Recently uploaded (20)

5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
 
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
 
➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men 🔝malwa🔝 Escorts Ser...
➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men  🔝malwa🔝   Escorts Ser...➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men  🔝malwa🔝   Escorts Ser...
➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men 🔝malwa🔝 Escorts Ser...
 
Call Girls In Nandini Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Nandini Layout ☎ 7737669865 🥵 Book Your One night StandCall Girls In Nandini Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Nandini Layout ☎ 7737669865 🥵 Book Your One night Stand
 
CHEAP Call Girls in Rabindra Nagar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Rabindra Nagar  (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Rabindra Nagar  (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Rabindra Nagar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Predicting Loan Approval: A Data Science Project
Predicting Loan Approval: A Data Science ProjectPredicting Loan Approval: A Data Science Project
Predicting Loan Approval: A Data Science Project
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
 
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night StandCall Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
 
➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men 🔝mahisagar🔝 Esc...
➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men  🔝mahisagar🔝   Esc...➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men  🔝mahisagar🔝   Esc...
➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men 🔝mahisagar🔝 Esc...
 
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
 
➥🔝 7737669865 🔝▻ Mathura Call-girls in Women Seeking Men 🔝Mathura🔝 Escorts...
➥🔝 7737669865 🔝▻ Mathura Call-girls in Women Seeking Men  🔝Mathura🔝   Escorts...➥🔝 7737669865 🔝▻ Mathura Call-girls in Women Seeking Men  🔝Mathura🔝   Escorts...
➥🔝 7737669865 🔝▻ Mathura Call-girls in Women Seeking Men 🔝Mathura🔝 Escorts...
 
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get CytotecAbortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
 
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
 
Just Call Vip call girls Bellary Escorts ☎️9352988975 Two shot with one girl ...
Just Call Vip call girls Bellary Escorts ☎️9352988975 Two shot with one girl ...Just Call Vip call girls Bellary Escorts ☎️9352988975 Two shot with one girl ...
Just Call Vip call girls Bellary Escorts ☎️9352988975 Two shot with one girl ...
 
Call Girls In Shivaji Nagar ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Shivaji Nagar ☎ 7737669865 🥵 Book Your One night StandCall Girls In Shivaji Nagar ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Shivaji Nagar ☎ 7737669865 🥵 Book Your One night Stand
 
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
 
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
 
Just Call Vip call girls kakinada Escorts ☎️9352988975 Two shot with one girl...
Just Call Vip call girls kakinada Escorts ☎️9352988975 Two shot with one girl...Just Call Vip call girls kakinada Escorts ☎️9352988975 Two shot with one girl...
Just Call Vip call girls kakinada Escorts ☎️9352988975 Two shot with one girl...
 
Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...
Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...
Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...
 

Lock basicsexamples

  • 1. IF EXISTS( SELECT 1 FROM sys.views WHERE name = 'DBlocks') DROP VIEWDBlocks; GO CREATE VIEW DBlocksAS SELECT request_session_idASspid, DB_NAME(resource_database_id) ASdbname , 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) END ASentity_name , index_id, resource_type ASresource , resource_descriptionASdescription, request_modeASmode , request_statusASstatus FROM sys.dm_tran_lockst LEFT JOIN sys.partitionsp ON p.partition_id=t.resource_associated_entity_id WHERE resource_database_id=DB_ID() ANDresource_type <>'DATABASE' ; GO
  • 2. -- Example 1: SELECT withREAD COMMITTED -- isolationlevel USE General; SET TRANSACTION ISOLATION LEVELREADCOMMITTED ; BEGIN TRAN SELECT * FROM dbo.GenderNG WHERE gendername ='Reflector'; SELECT * FROM DBlocks WHERE spid= @@spid; COMMIT TRAN /*There are nolockson the data in the Production.Producttable because the batch was doingonlySELECT operations,andsoacquiredonlySlocks.By default,SQLServer releasesSlocksassoon as ithas finishedreadingthe dataso,by the time we execute the SELECT fromthe view,SQLServerno longerholdsthe locks. */ -- Example 2: SELECT withREPEATABLE READ -- isolationlevel
  • 3. USE General ; SET TRANSACTION ISOLATION LEVELREPEATABLEREAD ; BEGIN TRAN SELECT gendername,sex FROM dbo.GenderNG WHERE gendername LIKE'Ana%'; SELECT * FROM DBlocks WHERE spid= @@spid ANDentity_name ='GenderNG'; COMMIT TRAN /* Thistime,because the transactionisolationlevelisREPEATABLEREAD,SQL Serverholds the S locksuntil the transactionisfinishedandsowe can see theminour results.The dbo.GenderNGtable hasaclusteredindex,sothe rowsof data are all index rows inthe leaf level.Assuch,the locksonthe 19 individual datarows returnedare KEY locks.The table alsohas a non-clusteredindexonthe genderNamecolumn and we can see 19 KEY locksat the leaf level of thisnon-clusteredindex,usedtofindthe relevantrows. */ -- Example 3: SELECT withSERIALIZABLEisolation -- level
  • 4. USE General ; SET TRANSACTION ISOLATION LEVELSERIALIZABLE; BEGIN TRAN SELECT * FROM dbo.GenderNG WHERE gendername LIKE'Ana%'; SELECT * FROM DBlocks WHERE spid= @@spid ANDentity_name ='GenderNG'; COMMIT TRAN /* The two-partmode RangeS-Sindicatesakey-range lockinadditiontothe lockon the keyitself.The firstpart(RangeS) isthe lockonthe range of keysbetweenandincluding the keyholdingthe lockandthe previouskeyinthe index.The key-rangelocksprevent othertransactionsfrominsertinganynew rowsintothe table thatmeetthe conditionof thisquery;that is,it'snot possible toinsertanynew rowswithaproduct name starting withRacingSocks.The key-range locksare heldonrangesinthe non-clusteredindex on gendername (Index_id=3) because thatisthe index usedto findthe qualifyingrows. */ -- Example 4: Update withREAD COMMITTED -- isolationlevel
  • 5. USE General ; SET TRANSACTION ISOLATION LEVELREADCOMMITTED ; BEGIN TRAN UPDATE dbo.GenderNG SET Sex = 1 WHERE gendername LIKE'Adam%'; SELECT * FROM DBlocks WHERE spid= @@spid ANDentity_name ='GenderNG'; COMMIT TRAN /* the two rowsinthe leaf level of the clusteredindex are lockedwith X locks.The page and the table are thenlockedwithIXlocks. As discussedearlier,SQLServeracquiresUlockswhile itlooksforthe rowsto update. However,SQLServerescalatesthese toXlocksuponperformingthe actual update and, by the time we lookat the DBlocksview,the U locksare gone.Unlesswe force U locks witha queryhint,we mightneversee theminthe lockreportfromDBlocks,or by direct inspectionof sys.dm_tran_locks. */ -- Example 5: Update withSERIALIZABLEisolation -- level (withanindex)
  • 6. USE General ; SET TRANSACTION ISOLATION LEVELSERIALIZABLE BEGIN TRAN UPDATE dbo.GenderNG SET Sex = 1 WHERE gendername LIKE'Adam%'; SELECT * FROM DBlocks WHERE spid= @@spid ANDentity_name ='GenderNG'; COMMIT TRAN /* Again,notice thatthe key-range locksare onthe non-clusteredindex,usedtofindthe relevantrows.The range interval itself needsonlyanSlockto preventinsertions,but the searchedkeyshave U locks,ensuringthatnootherprocesscan attempttoUPDATE them.The keysinthe table itself (index_id= 1) obtainthe X lockwhenthe actual modificationismade. */ -- Example 6: Update withSERIALIZABLEisolation -- level notusinganindex USE General ; SET TRANSACTION ISOLATION LEVELSERIALIZABLE
  • 7. BEGIN TRAN UPDATE dbo.GenderNG SET Sex = 1 WHERE Sex = 2 ; SELECT * FROM DBlocks WHERE spid= @@spid ANDentity_name ='GenderNG'; COMMIT TRAN /* As there wasno useful index,aclusteredindexscanonthe entire table wasrequired, and so all keysinitiallyreceivedthe RangeS-Ulock;whenfourrowswere eventually modified,the locksonthose keysescalatedtothe RangeX-Xlock.We cansee twoof the RangeX-Xlocks,anda fewof the RangeS-Ulocks.The complete outputhas501 RangeS-Ulocks,as well asIU lockson several pages,IXlocksontwopages,and an IX lockon the table. */ -- Example 7: Creatinga table USE General ; SET TRANSACTION ISOLATION LEVELREADCOMMITTED ; BEGIN TRAN SELECT *
  • 8. INTO newGender FROM dbo.GenderNG WHERE Sex BETWEEN 1 AND 2 ; SELECT * FROM DBlocks WHERE spid= @@spid; COMMIT TRAN /* SQL Serveracquiredveryfewof these locksonelementsof the newGendertable.Inthe entity_name column,note thatmostof the objectsare undocumented, andnormallyinvisible,systemtable names.Whencreatingthe new table, SQL Serveracquireslocksonsix differentsystemtablestorecordinformationabout thisnewtable.Inaddition,notice the schemamodification(Sch-M) locks */ -- Example 8: RID locks USE General ; SET TRANSACTION ISOLATION LEVELREADCOMMITTED BEGIN TRAN UPDATE newGender SET Sex = 1 WHERE genderName ='Alan'; SELECT * FROM DBlocks
  • 9. WHERE spid= @@spid ANDentity_name ='newGender'; COMMIT TRAN /* There are noindexesonthe newProductstable,sothe lockonthe actual row meeting our criterionisan Xlock on the row(RID).For RID locks,the descriptionactuallyreports the specificrowinthe form File Number:Page number:Slotnumber.Asexpected,SQL ServertakesIXlockson the page and the table. */