The above mentioned articles transaction, locking, blocking, and finally there is a more extreme case, is the deadlock, which is locked, a situation blocked.
Deadlock is when two transactions were locked resources, but continue to request other resources acquired, it will produce a deadlock.
Deadlock occurs because of:
A, sequential access session to a different table.
B, Session long-running transactions, update in one transaction or a lot of table rows, thus increasing the potential for conflict.
C, Session 1 apply some row locks, session 2 to apply some row locks, then decided to escalate to a table lock.
If these rows of the same data page, and two sessions to upgrade the lock granularity in the same page, it will deadlocks.
1, Session 1
-
set lock_timeout
1000
-
--跟踪死锁--会话1
-
set
transaction
isolation
level
serializable
-
-
begin tran
-
-
update t
-
set v =
'563'
-
where idd =
2
-
-
waitfor delay
'00:00:10'
-
-
update t
-
set v =
'963'
-
where idd =
1
-
-
COMMIT
2, Session 2
-
set
transaction
isolation
level
serializable
-
-
begin tran
-
-
update t
-
set v =
'234'
-
where idd =
1
-
-
waitfor delay
'00:00:10'
-
-
update t
-
set v =
'987'
-
where idd=
2
-
-
commit
3, then open a conversation, open track
Open trace flag:
the DBCC TRACEON (the trace # [, ... n-], -. 1) [With NO_INFOMSGS]
Check one or some flag is on or off:
the DBCC TRACESTATUS (the trace # [, ... n-], -. 1) [With NO_INFOMSGS]
1.trace #: specify one or more of the need to open or to check the status of the trace flags digit
2 -1: If -1 is specified globally open places one or some trace flag
3.with No_InfoMsgs: when this parameter is included in the command, DBCC output messages is prohibited
-
-
--跟踪1222能把详细的死锁信息返回到SQL Server的日志中
-
--标志位-1表示跟踪标志位1222应该对所有SQL Server连接全局启用
-
DBCC TraceOn(1222,-1)
-
go
-
-
--验证标志位是否启动
-
DBCC TraceStatus
-
go
-
-
--关闭标志位
-
DBCC TraceOff(1222,-1)
-
go
4, set the deadlock priority - the possibility of setting deadlock priority, adjust a query session is terminated due to a deadlock running
SET DeadLock_Priority Low | Normal | High | numeric-priority
-
--是当前连接很有可能被终止运行
-
set deadlock_priority
Low
-
-
--SQL Server终止回滚代价较小的连接
-
set deadlock_priority
Normal
-
-
--减少连接被终止的可能性,除非另一个连接也是High或数值优先级大于5
-
set deadlock_priority
High
-
-
--数值优先级:-10到10的值,-10最有可能被终止运行,10最不可能被终止运行,
-
--两个数字谁大,谁就越不可能在死锁中被终止
-
set deadlock_priority
10