Pages

Wednesday, September 17, 2014

LOCK vs Block vs Deadlock in SQL Server

Question :
What is the main difference between Lock , Block and Deadlock in SQL Server ?
Answer :
The Meaning of lock is :
Lock is a done by database when any connection access a same piece of data concurrently. One connection need to access Piece of data .

The Meaning of Block :
It occurs when two connections need access to same piece of data concurrently and the meanwhile another is blocked because at a particular time, only one connection can have access. SQL knows that once the blocking process finishes the resource will be available and so the blocked process will wait (until it times out), but it won’t be killed.

The Meaning of Deadlock :
Deadlock occurs when one connection is blocked and waiting for a second to complete its work, and this situation is again with another process as it waiting for first connection to release the lock. Hence deadlock occurs.
Example :
i have 2 processes. P1 & P2 trying to get to 2 resources R1 & R2.
P1 gets a lock on R1
and
P2 gets a lock on R2
THEN
P1 tries to get a lock on R2 but can’t because it is locked by P2
and
P2 tries to get a lock on R1 but can’t because it is locked by P1
in this point no process can finish because they are waiting on locked resources.  they are deadlocked. One of them must be killed to allow either of them to finish.

1 comment:

  1. Did you know that you can make money by locking special areas of your blog / website?
    Simply join CPALead and use their Content Locking tool.

    ReplyDelete