Presentation is loading. Please wait.

Presentation is loading. Please wait.

Transactions and Exception Handling Eric Allsop SQLBits 6 th October 2007.

Similar presentations


Presentation on theme: "Transactions and Exception Handling Eric Allsop SQLBits 6 th October 2007."— Presentation transcript:

1 Transactions and Exception Handling Eric Allsop SQLBits 6 th October 2007

2 Transactions Lock Manager Locks, lockable resources and modes Managing locks Concurrency vs. Isolation Blocks and Deadlocks Old School Exception Handling TRY/CATCH Managing Exceptions in Transactions Transactions and Exception Handling

3 Atomicity Consistency Isolation Durability What is a Transaction?

4 Implementation –Auto commit –Implicit (IMPLICIT_TRANSACTION) –Explicit (BEGAN TRAN etc.) Scope –Local –Distributed Types of Transaction

5 Transaction Processing

6 Internal SQL Server service Manages access to resources Services lock and latch requests Enforces isolation level In memory service Fixed memory allocation Lock Manager

7 Common Access –RID / KEY –Page –Table –Database Space Management –Extent –HOBT –Allocation Unit –File Other –Application –Metadata Lockable Resources Hierarchy of lockable resources Locks applied at most suitable level to maximise concurrency Automatic lock escalation driven by memory limits Lock hints

8 Existing Granted Mode Requested ModeISSUIXSIXX Intent shared (IS)   Shared (S)   Update (U)   Intent exclusive (IX)     Shared with intent exclusive (SIX)   Exclusive (X)  Lock Modes Shared locks (S) Exclusive locks (X) Lock compatibility Update locks (U) Intent locks (IS, IX, SIX, IU, SIU, UIX)

9 Schema locks (Sch-S, Sch-M) Bulk Update locks (BU) Key Range locks (RangeS-S, …) Lock hints Lock Modes contd.

10 Pre 2005 –sp_lock –sp_who / sp_who2 –DBCC INPUTBUFFER / fn_get_sql SQL 2005 –sys.dm_tran_locks –sys.dm_exec_connections –sys.dm_exec_requests –sys.dm_exec_sql_text Viewing Locking Information

11 Lost updates Uncommitted dependencies (Dirty reads) Inconsistent Analysis (Non repeatable reads) Phantom Reads Concurrency Effects

12 Pessimistic – use locks –Lower concurrency –Blocking –Deadlocking Optimistic – use snapshots –Higher concurrency –Can be resource intensive –May need to manage conflict Concurrency Model

13 LevelConcurrency Model Dirty Reads Lost Updates Non repeatable Reads Phantoms Read Uncommitted Pessimistic Yes Read Committed Pessimistic NoYes Read Committed Snapshot Optimistic NoYes Repeatable Read Pessimistic No Yes SerializablePessimistic No SnapshotOptimistic No Isolation Levels

14 Lock wait <> Deadlock Deadlock is an irresolvable chain of blocking Lock manager automatically resolves deadlock by selecting deadlock victim Most deadlock situations can be architected out Resolving deadlocks Deadlocks

15 LOCK_TIMEOUT Read-only filegroups Nested transactions Save points Transaction marks XACT_ABORT @@TRANCOUNT Transaction Bits and Pieces

16 Error vs. exception Severity vs. error number Some errors are too severe to handle User defined errors Exception Handling

17 @@ERROR and @@ROWCOUNT are volatile Limited handling capabilities Limited information available Repetitive code blocks Unstructured code with GOTO Potential need to manage open transaction in caller Exception handling with the @@family

18 Put suspect code in TRY block Put exception handling in CATCH block CATCH block directly follows TRY block in same batch Manage many more exceptions Throw error to caller using RAISERROR Exception handling functions provide detail of exception TRY/CATCH Methodology

19 XACT_STATE 0 – no active transactions 1 – open committable transaction -1 – open doomed transaction Exceptions in Transactions

20 Transactions Lock Manager Locks, lockable resources and modes Managing locks Concurrency vs. Isolation Blocks and Deadlocks Old School Exception Handling TRY/CATCH Managing Exceptions in Transactions Transactions and Exception Handling

21 Resources –Inside MS SQL Server 2005 Series T-SQL Programming –Itzik Ben-Gan et al. The Storage Engine –Kalen Delaney –Books Online Contact –eric.allsop@imgroup.com Resources and Contact Details


Download ppt "Transactions and Exception Handling Eric Allsop SQLBits 6 th October 2007."

Similar presentations


Ads by Google