You are on page 1of 27

Managing Transaction and

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

 Ensuring the highest level of integrity and consistency


within a database

 Used when statements are processed

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

 Write-Ahead transaction log


 Flushing the page and dirty Pages

 Use of checkpoints
 Minimize what must be processed to recover

 Truncating the transaction log

 Shrinking the transaction log


 DBCC SHRINKDATABASE
 DBCC SHRINKFILE

Microsoft
Transaction Recovery and Checkpoints
Transaction Recovery Action Required

1 None

2 Roll forward

3 Roll back

4 Roll forward

5 Roll back

Checkpoint System Failure

Microsoft
Concurrency Control

 Pessimistic concurrency control


 High contention for data

 Optimistic concurrency control


 Low contention for data

Microsoft
Lockable Resources
Item Description

RID Row identifier

Key Row lock within an index

Page Data page or index page

Extent Group of pages

Table Entire table

Database Entire database

Microsoft
Types of Locks
 Basic locks
 Shared (LS)
 Exclusive (LX)
 Special situation locks
 Intent
 Update
 Schema
 Bulk update

Microsoft
Lock Compatibility

 Locks may or may not be compatible with other Locks


LS LX

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

 Issues in nesting transactions


 Allowed, but not recommended
 Use @@trancount to determine nesting level

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

∙ ALTER DATABASE ∙ INSERT


∙ CREATE ∙ OPEN
∙ DELETE ∙ REVOKE
∙ DROP ∙ SELECT
∙ FETCH ∙ TRUNCATE TABLE
∙ GRANT ∙ UPDATE
 Transaction must be explicitly completed with COMMIT or
ROLLBACK TRANSACTION

Microsoft
SQL Server Locking
 Concurrency Problems Prevented by Locks
 Dealing with Deadlocks

Microsoft
Concurrency Problems Prevented by Locks

 Lost update
 Two simultaneous updates

 Uncommitted dependency (Dirty Read)


 Selecting data that’s being updated

 Inconsistent analysis (Nonrepeatable Read)


 Reading data that changed between multiple reads

 Phantoms reads
 Reading data that doesn’t exist on the second read

Microsoft
Concurrency Control

 Optimistic concurrency control


 Low contention for data

 Pessimistic concurrency control


 High contention for data

 Isolation level issues


 Lower isolation level: increases concurrency; increases chance
of reading bad data
 Higher isolation level: ensure data is correct; negatively affects
concurrency

Microsoft
Transaction Isolation Level

 READ COMMITTED (DEFAULT)


 Prevents SELECT statements from reading dirty, or uncommitted data

 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

Row Page Table


Locking Cost Granularity
Concurrency Cost
Microsoft
Blocks …

Microsoft
… Deadlocks
 Occurs when two transactions are blocking each other

Microsoft
Deadlock
 How SQL server ends a deadlock

 How to minimize deadlocks


 Access objects in same order
 Keep transactions short
 Use low isolation level
 Use bound connections

 How to customize the lock time-out setting


 SET LOCK_TIMEOUT function

Microsoft
Remarks

 Keep transactions short

 Design transactions to minimize deadlocks

 Use SQL Server defaults for locking

 Be careful when use locking options

Microsoft
Microsoft
Microsoft

You might also like