Download presentation
Presentation is loading. Please wait.
Published byLambert Sutton Modified over 9 years ago
1
Managing Transaction and Lock Vu Tuyet Trinh trinhvt-fit@mail.hut.edu.vn Hanoi University of Technology 1
2
Microsoft 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
3
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
4
Microsoft Overview Transaction Review of Transaction Log Introduction to Transactions and Locks Managing Transactions SQL Server Locking & Managing Locks
5
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
6
Microsoft Transaction Recovery and Checkpoints Transaction RecoveryAction Required None CheckpointSystem Failure 1 2 3 4 5 Roll forward Roll back Roll forward Roll back
7
Microsoft Concurrency Control Pessimistic concurrency control High contention for data Optimistic concurrency control Low contention for data
8
Microsoft Lockable Resources Item Item Description Description RIDRow identifier KeyRow lock within an index Page Extent Table Data page or index page Group of pages Entire table DatabaseEntire database
9
Microsoft Types of Locks Basic locks Shared (LS) Exclusive (LX) Special situation locks Intent Update Schema Bulk update
10
Microsoft 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 LSLX LStruefalse LXfalse
11
Microsoft Managing Locks Session-level locking options – READ UNCOMMITTED – READ COMMITTED (default) – REPEATABLE READ – SERIALIZABLE – SNAPSHOT Locking timeout – Limits time waiting for a locked resource – SET LOCK_TIMEOUT
12
Microsoft Viewing Locking Information Activity Monitor sys.dm_tran_locks Dynamic Management View EnumLocks SQL Server Profiler System Monitor
13
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
14
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
15
Microsoft Explicit Transaction Defining BEGIN TRANSACTION COMMIT TRANSACTION ROLLBACK TRANSACTION BEGIN TRANSACTION INSERT INTO Extensions (PrimaryExt) VALUES ('555–1212') INSERT INTO Extensions (VoiceMailExt) VALUES ('5551212') ROLLBACK TRANSACTION
16
Microsoft Implicit Transaction Using An implicit transaction starts when one of the following statements is executed Transaction must be explicitly completed with COMMIT or ROLLBACK TRANSACTION SET IMPLICIT_TRANSACTIONS ON ∙ ALTER DATABASE ∙ CREATE ∙ DELETE ∙ DROP ∙ FETCH ∙ GRANT ∙ INSERT ∙ OPEN ∙ REVOKE ∙ SELECT ∙ TRUNCATE TABLE ∙ UPDATE
17
Microsoft SQL Server Locking Concurrency Problems Prevented by Locks Dealing with Deadlocks
18
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
19
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
20
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
21
Microsoft Dynamic Locking Architecture Dynamic Locking TablePageRow Cost Granularity Locking Cost Concurrency Cost
22
Microsoft Blocks …
23
Microsoft … Deadlocks Occurs when two transactions are blocking each other
24
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
25
Microsoft Remarks Keep transactions short Design transactions to minimize deadlocks Use SQL Server defaults for locking Be careful when use locking options
26
Microsoft
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.