Professional Documents
Culture Documents
Lock
Vu Tuyet Trinh
trinhvt-fit@mail.hut.edu.vn
Hanoi University of Technology
1
Transaction
A sequence of tasks that together constitute a logical unit
of work
Microsoft
ACID Properties
Atomicity
All or none
Consistency
Leave data in a consistent state
Isolation
Don’t show data that’s ½ done
Durability
Modifications are permanent
Microsoft
Overview
Transaction
Review of Transaction Log
Introduction to Transactions and Locks
Managing Transactions
SQL Server Locking & Managing Locks
Microsoft
Transaction Log Architecture
Use of checkpoints
Minimize what must be processed to recover
Microsoft
Transaction Recovery and Checkpoints
Transaction Recovery Action Required
1 None
2 Roll forward
3 Roll back
4 Roll forward
5 Roll back
Microsoft
Concurrency Control
Microsoft
Lockable Resources
Item Description
Microsoft
Types of Locks
Basic locks
Shared (LS)
Exclusive (LX)
Special situation locks
Intent
Update
Schema
Bulk update
Microsoft
Lock Compatibility
LS true false
LX false false
Examples
Shared locks are compatible with all locks except exclusive
Exclusive locks are not compatible with any other locks
Update locks are compatible only with shared locks
Microsoft
Managing Locks
Session-level locking options
– READ UNCOMMITTED
– READ COMMITTED (default)
– REPEATABLE READ
– SNAPSHOT
– SERIALIZABLE
Locking timeout
– Limits time waiting for a locked resource
– SET LOCK_TIMEOUT
Microsoft
Viewing Locking Information
• Activity Monitor
• sys.dm_tran_locks Dynamic Management View
• EnumLocks
• SQL Server Profiler
• System Monitor
Microsoft
Considerations for Using Transactions
Transaction guidelines
Keep transactions as short as possible
Use caution with certain Transact-SQL statements
Avoid transactions that require user interaction
Microsoft
Transaction Types
Explicit Transaction
Explicitly define start and end
Autocommit Transactions
Every statement is committed or rolled back
Implicit Transactions
Statement after “Commit Transaction” or Rollback Transaction”
starts a new transaction
SET IMPLICIT_TRANSACTIONS ON
Microsoft
Explicit Transaction
Defining
BEGIN TRANSACTION ROLLBACK TRANSACTION
COMMIT TRANSACTION
BEGIN TRANSACTION
INSERT INTO Extensions (PrimaryExt)
VALUES ('555–1212')
INSERT INTO Extensions (VoiceMailExt)
VALUES ('5551212')
ROLLBACK TRANSACTION
Microsoft
Implicit Transaction
Using SET IMPLICIT_TRANSACTIONS ON
An implicit transaction starts when one of the following
statements is executed
Microsoft
SQL Server Locking
Concurrency Problems Prevented by Locks
Dealing with Deadlocks
Microsoft
Concurrency Problems Prevented by Locks
Lost update
Two simultaneous updates
Phantoms reads
Reading data that doesn’t exist on the second read
Microsoft
Concurrency Control
Microsoft
Transaction Isolation Level
READ UNCOMMITTED
Allows dirty reads
REPEATABLE READ
No other rows can update/delete rows in the result set until released; inserting
news rows is okay
SERIALIZABLE
No other transaction can make any changes
Microsoft
Dynamic Locking Architecture
Dynamic
Locking
Cost
Microsoft
… Deadlocks
Occurs when two transactions are blocking each other
Microsoft
Deadlock
How SQL server ends a deadlock
Microsoft
Remarks
Microsoft
Microsoft
Microsoft