session10 transaction
TRANSCRIPT
-
8/7/2019 Session10 Transaction
1/27
Managing Transaction and
Lock
Vu Tuyet [email protected]
Hanoi University of Technology
1
-
8/7/2019 Session10 Transaction
2/27
Microsoft
Microsoft
Transaction
A sequence of tasks that together constitute a logicalunit of work
Ensuring the highest level of integrity and consistencywithin a database
Used when statements are processed
-
8/7/2019 Session10 Transaction
3/27
Microsoft
Microsoft
ACID Properties
Atomicity
All or none
Consistency Leave data in a consistent state
Isolation
Dont show data thats done
Durability
Modifications are permanent
-
8/7/2019 Session10 Transaction
4/27
Microsoft
Microsoft
Overview
Transaction
Review of Transaction Log
Introduction to Transactions and Locks Managing Transactions
SQL Server Locking & Managing Locks
-
8/7/2019 Session10 Transaction
5/27
Microsoft
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
-
8/7/2019 Session10 Transaction
6/27
Microsoft
Microsoft
Transaction Recovery and Checkpoints
Transaction Recovery Action Required
None
Checkpoint System Failure
11
22
33
44
55
Roll forward
Roll back
Roll forward
Roll back
-
8/7/2019 Session10 Transaction
7/27
MicrosoftMicrosoft
Concurrency Control
Pessimistic concurrency control
High contention for data
Optimistic concurrency control
Low contention for data
-
8/7/2019 Session10 Transaction
8/27
MicrosoftMicrosoft
Lockable Resources
ItemItem DescriptionDescription
RID Row identifier
Key Row lock within an index
Page
Extent
Table
Data page or index page
Group of pages
Entire table
Database Entire database
-
8/7/2019 Session10 Transaction
9/27
MicrosoftMicrosoft
Types of Locks
Basic locks
Shared (LS)
Exclusive (LX)
Special situation locks Intent
Update
Schema
Bulk update
-
8/7/2019 Session10 Transaction
10/27
MicrosoftMicrosoft
Lock Compatibility
Locks may or may not be compatible with other Locks
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
LS LX
LS true false
LX false false
-
8/7/2019 Session10 Transaction
11/27
MicrosoftMicrosoft
Managing Locks
Session-level locking options
READ UNCOMMITTED
READCOMMITTED (default) REPEATABLE READ
SERIALIZABLE
SNAPSHOT
Locking timeout
Limits time waiting for a locked resource
SET LOCK_TIMEOUT
-
8/7/2019 Session10 Transaction
12/27
MicrosoftMicrosoft
Viewing Locking Information
Activity Monitor
sys.dm_tran_locks Dynamic Management View
EnumLocks SQL Server Profiler
System Monitor
-
8/7/2019 Session10 Transaction
13/27
MicrosoftMicrosoft
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
-
8/7/2019 Session10 Transaction
14/27
MicrosoftMicrosoft
Transaction Types
Explicit Transaction
Explicitly define start and end
Autocommit Transactions
Every statement is committed or rolled back
Implicit Transactions
Statement after Commit Transaction orRollback Transaction
starts a new transaction
SET IMPLICIT_TRANSACTIONSON
-
8/7/2019 Session10 Transaction
15/27
MicrosoftMicrosoft
Explicit Transaction
DefiningBEGIN TRANSACTION
COMMIT TRANSACTION
ROLLBACK TRANSACTION
BEGIN TRANSACTION
INSERT INTO Extensions (PrimaryExt)
VALUES ('5551212')INSERT INTO Extensions (VoiceMailExt)
VALUES ('5551212')
ROLLBACK TRANSACTION
-
8/7/2019 Session10 Transaction
16/27
MicrosoftMicrosoft
Implicit Transaction
Using
An implicit transaction starts when one of the following
statements is executed
Transaction must be explicitly completed with COMMIT orROLLBACK TRANSACTION
SET IMPLICIT_TRANSACTIONS ON
ALTER DATABASE
CREATE
DELETE
DROP
FETCH
GRANT
INSERT
OPEN
REVOKE
SELECT
TRUNCATE TABLE
UPDATE
-
8/7/2019 Session10 Transaction
17/27
MicrosoftMicrosoft
SQL Server Locking
Concurrency Problems Prevented by Locks
Dealing with Deadlocks
-
8/7/2019 Session10 Transaction
18/27
MicrosoftMicrosoft
Concurrency Problems Prevented by Locks
Lost update
Two simultaneous updates
Uncommitted dependency (Dirty Read)
Selecting data thats being updated
Inconsistent analysis (Nonrepeatable Read)
Reading data that changed between multiple reads
Phantoms reads
Reading data that doesnt exist on the second read
-
8/7/2019 Session10 Transaction
19/27
MicrosoftMicrosoft
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 chanceof reading bad data
Higher isolation level: ensure data is correct; negatively affectsconcurrency
-
8/7/2019 Session10 Transaction
20/27
MicrosoftMicrosoft
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
-
8/7/2019 Session10 Transaction
21/27
MicrosoftMicrosoft
Dynamic Locking Architecture
DynamicLocking
TablePageRow
Cost
GranularityLocking Cost
ConcurrencyCost
-
8/7/2019 Session10 Transaction
22/27
MicrosoftMicrosoft
Blocks
-
8/7/2019 Session10 Transaction
23/27
MicrosoftMicrosoft
Deadlocks
Occurs when two transactions are blocking each other
-
8/7/2019 Session10 Transaction
24/27
MicrosoftMicrosoft
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
-
8/7/2019 Session10 Transaction
25/27
MicrosoftMicrosoft
Remarks
Keep transactions short
Design transactions to minimize deadlocks
Use SQL Server defaults for locking
Be careful when use locking options
-
8/7/2019 Session10 Transaction
26/27
MicrosoftMicrosoft
-
8/7/2019 Session10 Transaction
27/27
MicrosoftMicrosoft