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

Slides:



Advertisements
Similar presentations
Applied Database II Transactions In Database The ACID Test Atomicity The whole transaction or none of it Consistency Remains in a consistent state -
Advertisements

1 Lecture 11: Transactions: Concurrency. 2 Overview Transactions Concurrency Control Locking Transactions in SQL.
Accessing data Transactions. Agenda Questions from last class? Transactions concurrency Locking rollback.
Concurrency Control Part 2 R&G - Chapter 17 The sequel was far better than the original! -- Nobody.
1 Data Concurrency David Konopnicki 1997 Revised by Mordo Shalom 2004.
Data and Database Administration Chapter 12. Outline What is Concurrency Control? Background Serializability  Locking mechanisms.
Transactions and Locking Rose-Hulman Institute of Technology Curt Clifton.
Transactions and Locks
Module 15: Managing Transactions and Locks. Overview Introduction to Transactions and Locks Managing Transactions SQL Server Locking Managing Locks.
Database Administration Chapter Six DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Concurrency Control Managing Hierarchies of Database Elements (18.6) 1 Presented by Ronak Shah (214) March 9, 2009.
Database Administration Part 1 Chapter Six CSCI260 Database Applications.
Transactions and Locks Lesson 22. Skills Matrix Transaction A transaction is a series of steps that perform a logical unit of work. Transactions must.
Managing Transaction and Lock Vu Tuyet Trinh Hanoi University of Technology 1.
1 IT420: Database Management and Organization Transactions 31 March 2006 Adina Crăiniceanu
A Comedy of Errors Handling Errors in T-SQL Code Andrew Whettam.
1099 Why Use InterBase? Bill Todd The Database Group, Inc.
Oracle Locking Michael Messina Principal Database Analyst Indiana University.
Concurrency and Transaction Processing. Concurrency models 1. Pessimistic –avoids conflicts by acquiring locks on data that is being read, so no other.
Unit 9 Transaction Processing. Key Concepts Distributed databases and DDBMS Distributed database advantages. Distributed database disadvantages Using.
How transactions work A transaction groups a set of Transact-SQL statements so that they are treated as a unit. Either all statements in the group are.
Module 11 Creating Highly Concurrent SQL Server® 2008 R2 Applications.
Transactions and Locks A Quick Reference and Summary BIT 275.
© 2002 by Prentice Hall 1 Database Administration David M. Kroenke Database Concepts 1e Chapter 6 6.
Random Logic l Forum.NET l Transaction Isolation Levels Forum.NET Meeting ● Nov
Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness.
Module 6: Data Protection. Overview What does Data Protection include? Protecting data from unauthorized users and authorized users who are trying to.
Giovanni Chierico | May 2012 | Дубна Data Concurrency, Consistency and Integrity.
1 Advanced Database Concepts Transaction Management and Concurrency Control.
Module 11: Managing Transactions and Locks
Module 14: Managing Transactions and Locks. Overview Introducing Transactions and Locks Managing Transactions Understanding SQL Server Locking Architecture.
Maciej Pilecki Project Botticelli Ltd. Session Code: DAT402.
In this session, you will learn to: Implement triggers Implement transactions Objectives.
Locking, Blocking, Latching, OH MY! SQL SERVER:. About me  16 years of SQL Server experience  SQL 6.5, 7.0, 2000, 2005, 2008, 2008 R2, 2012, 2014 
Deadlocks 3.0. Final Edition. Everything that developer needs to know Denis Reznik Microsoft SQL Server MVP Director of R&D at Intapp Kyiv.
Does the Optimistic Concurrency resolve your blocking problems Margarita Naumova, SQL Master Academy.
Locks, Blocks & Isolation Oh My!. About Me Keith Tate Data Professional for over 14 Years MCITP in both DBA and Dev tracks
Read Dirty to Me: SQL Server Isolation Levels Wendy Pastrick Arrow IT Consulting.
SQLintersection Locks, Blocks, and Deadlocks Oh My! Randy Knight Wednesday, 2:15-3:15.
Database Transactions  Transaction Management and Concurrency Control.
Transactions in PostgreSQL
Concurrency Control Managing Hierarchies of Database Elements (18.6)
Database Transactions
11 | Error Handling and Transactions
Let Me Finish... Isolating Write Operations
Let Me Finish... Isolating Write Operations
Let Me Finish... Isolating Write Operations
Transaction Properties
On transactions, and Atomic Operations
Batches, Transactions, & Errors
Everything you ever wanted to ask but were too shy
මොඩියුල විශ්ලේෂණය Transactions කළමනාකරණය.
Transactions, Locking and Query Optimisation
On transactions, and Atomic Operations
Let Me Finish... Isolating Write Operations
Batches, Transactions, & Errors
Let Me Finish... Isolating Write Operations
Objectives Define and describe transactions
Transactions and Concurrency
Concurrency Control E0 261 Prasad Deshpande, Jayant Haritsa
Deadlocks Everything you ever wanted to ask but were too shy
Database Administration
A Beginners Guide to Transactions
A Beginners Guide to Transactions
A Masters view on Locking and blocking
A Beginners Guide to Transactions
About Wolf DBA for over 19 years At RDX for nearly 9
A Beginners Guide to Transactions
Module 13: Creating Highly Concurrent SQL Server 2012 Applications
A Masters view on Locking and blocking
Presentation transcript:

Transactions and Exception Handling Eric Allsop SQLBits 6 th October 2007

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

Atomicity Consistency Isolation Durability What is a Transaction?

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

Transaction Processing

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

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

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)

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

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

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

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

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

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

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

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

and 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

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

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

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

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