Batches, Transactions, & Errors John Deardurff Website: http://john.deardurff.com Twitter: @John_Deardurff Email: John@Deardurff.com December 12, 2015
Presentation Topics How Queries are Processed Types of Errors Working with Batches ACID Transactions Explicit Transactions Error Handling
Parse Resolve Optimize Compile Execute Execute SQL Sets Ad Hoc Query Stored Procedure Syntax Parse Execution Context Resolve Compile Optimize Execution Plan Procedure Cache Compile Run Time Execute Execute SQL Sets
Working with Batches
VS Batches vs. Transactions Batches Transactions TSQL2012.mdf Sends Code to the Processor Sends Data to the Database
Batches And Variables First Batch Successful Second Batch Fails
Transactions must pass the ACID test Atomicity – All or Nothing Consistent – Only valid data Isolated – No interference Durable – Data is recoverable
Transaction Recovery Transaction Recovery Action Required 1 None 2 Roll forward 3 Roll back 4 Roll forward 5 Roll back Checkpoint System Failure
Auto Commit Transactions without Error Handling TSQL2012.ldf Checkpoint TSQL2012.mdf
Explicit Transactions without Error Handling TSQL2012.ldf Checkpoint TSQL2012.mdf
Explicit Transactions with Error Handling TSQL2012.ldf TSQL2012.mdf Checkpoint
What are Locks? Transaction 1 Transaction 2 TSQL2012.mdf
Module 13: Creating Highly Concurrent SQL Server 2012 Applications Course 10776A What Problems does Locking Prevent? Module 13: Creating Highly Concurrent SQL Server 2012 Applications Lost Updates Uncommitted dependency (dirty read) Inconsistent analysis (non-repeatable reads) Phantom reads Question: Has your organization experienced concurrency problems with database applications? If so, what behavior did you see? Answer: Answers will vary. References: Concurrency Problems: http://go.microsoft.com/fwlink/?LinkID=233921 Concurrency Effects: http://go.microsoft.com/fwlink/?LinkID=233922 Locking in the Database Engine: http://go.microsoft.com/fwlink/?LinkID=233923
Lost Update Original value of 125. The second session is unaware of the first sessions update.
Uncommitted dependency (dirty read) Clean Read Dirty Read
Inconsistent analysis (non-repeatable read) Change Isolation Level to Repeatable Read
Phantom Reads – (Demo Setup) Added 3 columns for demo
Phantom Reads Missing record 18
Transaction Isolation Levels
Module 13: Creating Highly Concurrent SQL Server 2012 Applications Course 10776A Transaction Isolation Levels Module 13: Creating Highly Concurrent SQL Server 2012 Applications Isolation Level Dirty Read Lost Update Nonrepeatable Read Phantoms Read uncommitted Yes Read committed (default) No Repeatable read Serializable Snapshot Discuss the effects that each of the transaction isolation levels has on data access. Be sure that students understand the READ COMMITTED isolation level, and that it is the default transaction isolation level. References: SET TRANSACTION ISOLATION LEVEL (Transact-SQL): http://go.microsoft.com/fwlink/?LinkID=233927 Transaction Isolation Levels: http://go.microsoft.com/fwlink/?LinkID=209385