Example For DeadLock

The dead lock behavior would be like this. This is just for  understand purpose.   In the below i have chosen another tables.
                        Session1                                     Session2
1. Begin Transaction
1. Begin Transaction
2. Update Part table
2. Update Supplier table
3. Update Supplier table
3. Update Part table
4. Commit Transaction
4. Commit Transaction


CREATE TABLE authors
(id VARCHAR(20),Lname VARCHAR(30),Value1 VARCHAR(30),Value2 VARCHAR(30),Value3 VARCHAR(30),Value4 VARCHAR(30),Value5 VARCHAR(30),SomeValue VARCHAR(30),Sno INT)
GO
---1 st window
DECLARE @Au_ID VARCHAR(11)
DECLARE @Au_lname VARCHAR(30)
SELECT @Au_ID='111-11-1112',@Au_lname='Test1'
BEGIN TRAN
INSERT INTO authors
VALUES(@Au_ID,@Au_lname,' ',' ',' ',' ',' ','11111',0)
WAITFOR DELAY '00:00:05'
SELECT * FROM Authors
WHERE Lname like 'Test%'
COMMIT
------2nd Window
DECLARE @Au_ID VARCHAR(11)
DECLARE @Au_lname VARCHAR(30)
SELECT @Au_ID='111-11-1111',@Au_lname='Test2'
BEGIN TRAN
INSERT INTO authors
VALUES(@Au_ID,@Au_lname,' ',' ',' ',' ',' ','11111',0)
WAITFOR DELAY '00:00:05'
SELECT * FROM Authors
WHERE Lname like 'Test%'COMMIT
--3rd window
DELETE FROM authors
WHERE au_id = '111-11-1111'
DELETE FROM authors
WHERE au_id = '111-11-1112'


------Some of the links which discuss about DEADLOCK
http://social.msdn.microsoft.com/Forums/en-US/sqldatabaseengine/thread/21ab17f7-9162-4a1f-9286-6cccd2d192a2

And we have also another lock called LIVE LOCK. We can reproduce this in the below way.

--Session1
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
BEGIN TRAN
SELECT * FROM Customers

--Session 2
Run the below command without any locking and isolation levels

UPDATE Customers
SET FirstName='SomeValue'
WHERE CustomerNumber='SomeValue'

And you can see the above UPDATE command is going to take tim this way.

Comments

Popular posts from this blog

System.Data.SqlClient.SqlException (0x80131904): Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding. ---> System.ComponentModel.Win32Exception (0x80004005): The wait operation timed out

Pre login Handshake or Connection Timeout Period

Transparent Data Encryption(TDE) with Master Key and Certificate in SQL Server