Deadlock:
Deadlock occurs when two or more SQL Server processes have locks on separate database objects and each process is trying to acquire a lock on an object that the other processes have previously locked. For example, process one has an exclusive lock on object one, process two has an exclusive lock on object two, and process one also wants an exclusive lock on object two, and object two wants an exclusive lock on object one. Because two processes can’t have an exclusive lock on the same object at the same time, the two processes become entangled in a deadlock.
SQL Server is smart enough to identify the problem and ends the deadlock by choosing one process over another. It does this by killing one of the processes (usually the process that has used the least amount of server resources up to this point) and lets the other one to continue to run.
How to Deal with:
Simply whenever somebody is writing T-SQL code use objects in order.
whenever you are feeling DEADLOCK victim's check with other transactions
and provide DEADLOCK_PRIORITY for that.
in SQL 2K it was trace flags - 1204 & 1222, in 2k5 & onwards Microsoft have
introduced new events were added in SQL Server Profiler.
I am exaplaining some of the important events (we can use):
· Deadlock graph
· Lock: Deadlock
· Lock: Deadlock Chain
· RPC:Completed
· SP:StmtCompleted
· SQL:BatchCompleted
· SQL:BatchStarting
Deadlock Graph
Of seven events I have listed above, the only event you must have is the Deadlock Graph event. It captures, in both XML format and graphically, a drawing that shows you exactly the cause of the deadlock. We will examine how to interpret this drawing later in this article.
Lock:Deadlock
This event is fired whenever a deadlock occurs, and because of this, it is also fired every time the Deadlock Graph event is fired, producing redundant data. I have included it here because it makes it a little easier to see what is happening, but if you like, you can drop this event from your trace.
Lock:Deadlock Chain
This event is fired once for every process involved in a deadlock. In most cases, a deadlock only affects two processes at a time, and because of this, you will see this event fired twice just before the Deadlock Graph and the Lock:Deadlock events fire. In rare cases, more than two processes are involved in a deadlock, and if this is the case, an event will be fired for every process involved in the deadlock.
RPC: Completed
The RPC: Completed event fires after a stored procedure is executed as a remote procedure call. It includes useful information about the execution of the stored procedure, including the CPU time used to execute the stored procedure, the total length of time the stored procedure ran, logical reads and writes that occurred during its execution, along with the name of the stored procedure itself.
SP: StmtCompleted
Stored procedures are made up of one or more statements. In SQL Server 2005, each statement within a stored procedure is traced. The SP: StmtCompleted event indicates when a statement within a stored procedure has ended. The StmtCompleted event’s data columns provide lots of useful information about the statement, including the actual code in the statement, the duration the statement ran, the amount of CPU time used, the number of logical reads and writes, the number of rows returned by the statement, among others.
SQL: BatchStarting
The SQL: BatchStarting event is fired whenever a new batch begins. Once a batch begins, then one or more individual Transact-SQL statements occur. The SQL: BatchStarting event is a good event to easily see where a batch begins, but other than this, it is not particularly useful.
SQL: BatchCompleted
The SQL: BatchCompleted event occurs when a batch completes. This means that one or more Transact-SQL statements have completed for the batch. The SQL: BatchCompleted event is more useful than the SQL: BatchStarting event because it includes useful information like the duration of the entire batch, the logical number of reads and writes caused by all the statements inside the batch, the total number of rows returned by the batch, and other useful information.
Some of the keypoints in reducing DeadLocks
1) Ensure Database design is properly normalized
2) As i have mentioned above objects in the same order.
3) Keep transaction as short as possible.
4) During Transactions - dont allow user input/user interface
5) Avoid using cursors as much as you can....there are lot of alternatives available now.
6) Use NOLOCK hint to prevent locking
7) Use low level of isolation as possible for user connections.
8) reduce lock escalation by using the ROWLOCK or PAGLOCK hint.
Blocking:
Blocking is normal consequence of locking.
Blocking occurs in SQL Server 2005 when one process has one or more records locked, while another process tries to acquire locks on the same set (or subset) of records. This causes the second process to wait until the first process is done so it can acquire the records; this can cause other processes to be blocked if they are waiting on the resources that the second connection has acquired. These processes are ultimately dependent upon the first process to complete its work before they can complete theirs. This can create a daisy-chain of processes waiting to complete their work.
There is thin line between Blocking and Deadlocking but it is require to understand.
This blocking situation is a bit different than a deadlock scenario. A deadlock occurs when process A has ownership of resources that process B needs. Process B, in turn, owns resources that process A needs to complete its work. In theory, this would last forever. Fortunately, SQL Server 2005 has algorithms to find these scenarios and stop one process so the other can finish its work. On the other hand, blocking can last a lot longer than a deadlock because it isn’t stopped automatically by SQL Server 2005. The blocked processes must wait until the blocking connection finishes its work before they can finish theirs. So i can say BLOCKING more dangerous than DEADLOCK.
How to deal with BLOCKING:
Fetching from sysprocesses where blocked >0.
using 2k5 and onwards enhancements -
SELECT
s.spid, BlockingSPID = s.blocked, DatabaseName = DBNAME(s.dbid),
s.programname, s.loginame, ObjectName = OBJECTNAME(objectid,s.dbid), Definition = CAST(text AS VARCHAR(MAX))
INTO #Processes
FROM sys.sysprocesses s
CROSS APPLY sys.dmexecsqltext (sql_handle)
WHERE
s.spid > 50
To determine whether lock escalation is occurring around the time when you experience blocking issues, start a SQL Profiler trace that includes the Lock:Escalation event. Check Trace Flag -T1211.
LiveLock:
A Live lock is one, where a request for exclusive lock is denied continuously because a series of overlapping shared locks keeps on interfering each other and to adapt from each other they keep on changing the status which further prevents them to complete the task. In SQL Server Live Lock occurs when read transactions are applied on table which prevents write transaction to wait indefinitely. This is different then deadlock as in deadlock both the processes wait on each other.
In SQL Server Live Lock occurs when read transactions are applied on table which prevents write transaction to wait indefinitely. This is different then deadlock as in deadlock both the processes wait on each other.
Giving one of the example i am having
–1
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
BEGIN TRAN
SELECT * FROM authors
–2
UPDATE authors
SET aulname = ‘X’
WHERE auid = ’238-95-7766′
–3
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
BEGIN TRAN
SELECT * FROM authors
–4
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
BEGIN TRAN
SELECT * FROM authors
–5
EXEC sp_who2
You will see that 2 is blocked by 1, 3 is blocked by 2 and 4 is blocked by 3. Theoretically, 3 and 4 could execute (there’s only a shared lock), but queuing makes 3 and 4 to wait. This is how livelocks are avoided: queuing.