module 15: managing transactions and locks. overview introduction to transactions and locks managing...
Post on 19-Dec-2015
226 views
TRANSCRIPT
![Page 1: Module 15: Managing Transactions and Locks. Overview Introduction to Transactions and Locks Managing Transactions SQL Server Locking Managing Locks](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d385503460f94a12376/html5/thumbnails/1.jpg)
Module 15: Managing Transactions
and Locks
![Page 2: Module 15: Managing Transactions and Locks. Overview Introduction to Transactions and Locks Managing Transactions SQL Server Locking Managing Locks](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d385503460f94a12376/html5/thumbnails/2.jpg)
Overview
Introduction to Transactions and Locks
Managing Transactions
SQL Server Locking
Managing Locks
![Page 3: Module 15: Managing Transactions and Locks. Overview Introduction to Transactions and Locks Managing Transactions SQL Server Locking Managing Locks](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d385503460f94a12376/html5/thumbnails/3.jpg)
Introduction to Transactions and Locks
Transactions Ensure That Multiple Data Modifications Are Processed Together
Locks Prevent Update Conflicts
Transactions are serializable
Locking is automatic
Locks allow concurrent use of data
Concurrency Control
![Page 4: Module 15: Managing Transactions and Locks. Overview Introduction to Transactions and Locks Managing Transactions SQL Server Locking Managing Locks](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d385503460f94a12376/html5/thumbnails/4.jpg)
Managing Transactions
Multimedia Presentation: SQL Server Transactions
Transaction Recovery and Checkpoints
Considerations for Using Transactions
Setting the Implicit Transactions Option
Restrictions on User-defined Transactions
![Page 5: Module 15: Managing Transactions and Locks. Overview Introduction to Transactions and Locks Managing Transactions SQL Server Locking Managing Locks](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d385503460f94a12376/html5/thumbnails/5.jpg)
Multimedia Presentation: SQL Server Transactions
![Page 6: Module 15: Managing Transactions and Locks. Overview Introduction to Transactions and Locks Managing Transactions SQL Server Locking Managing Locks](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d385503460f94a12376/html5/thumbnails/6.jpg)
Transaction Recovery and Checkpoints
Tim
e (a
nd p
lace
in lo
g)
Database
Transaction Log
Transaction LogINSERT …DELETE …UPDATE ……
INSERT …DELETE …UPDATE ……
INSERT …DELETE …UPDATE ……
INSERT …DELETE …UPDATE ……
INSERT …DELETE …UPDATE ……
CHECKPOINT
CRASH!!!
COMMIT
COMMIT
COMMIT
Recovery Needed? NONERecovery Needed? NONE
Recovery Needed? ROLL FORWARDRecovery Needed? ROLL FORWARD
Recovery Needed? ROLL BACKRecovery Needed? ROLL BACK
Recovery Needed? ROLL FORWARDRecovery Needed? ROLL FORWARD
Recovery Needed? ROLL BACKRecovery Needed? ROLL BACK
ZOT!
![Page 7: Module 15: Managing Transactions and Locks. Overview Introduction to Transactions and Locks Managing Transactions SQL Server Locking Managing Locks](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d385503460f94a12376/html5/thumbnails/7.jpg)
Considerations for Using Transactions
Transaction Guidelines
Keep transactions as small 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
![Page 8: Module 15: Managing Transactions and Locks. Overview Introduction to Transactions and Locks Managing Transactions SQL Server Locking Managing Locks](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d385503460f94a12376/html5/thumbnails/8.jpg)
Setting the Implicit Transactions Option
Automatically Starts a Transaction When You Execute Certain Statements
Nested Transactions Are Not Allowed
Transaction Must Be Explicitly Completed with COMMIT or ROLLBACK TRANSACTION
By Default, Setting Is Off
SET IMPLICIT_TRANSACTIONS ONSET IMPLICIT_TRANSACTIONS ON
![Page 9: Module 15: Managing Transactions and Locks. Overview Introduction to Transactions and Locks Managing Transactions SQL Server Locking Managing Locks](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d385503460f94a12376/html5/thumbnails/9.jpg)
ALTER DATABASE BACKUP LOG CREATE DATABASE DROP DATABASE
RECONFIGURE RESTORE DATABASE RESTORE LOG UPDATE STATISTICS
Restrictions on User-defined Transactions
Certain Statements May Not Be Included in a Transaction
![Page 10: Module 15: Managing Transactions and Locks. Overview Introduction to Transactions and Locks Managing Transactions SQL Server Locking Managing Locks](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d385503460f94a12376/html5/thumbnails/10.jpg)
SQL Server Locking
Concurrency Problems Prevented by Locks
Lockable Resources
Types of Locks
Lock Compatibility
![Page 11: Module 15: Managing Transactions and Locks. Overview Introduction to Transactions and Locks Managing Transactions SQL Server Locking Managing Locks](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d385503460f94a12376/html5/thumbnails/11.jpg)
Concurrency Problems Prevented by Locks
Lost Update
Uncommitted Dependency (Dirty Read)
Inconsistent Analysis (Nonrepeatable Read)
Phantoms Reads
![Page 12: Module 15: Managing Transactions and Locks. Overview Introduction to Transactions and Locks Managing Transactions SQL Server Locking Managing Locks](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d385503460f94a12376/html5/thumbnails/12.jpg)
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
![Page 13: Module 15: Managing Transactions and Locks. Overview Introduction to Transactions and Locks Managing Transactions SQL Server Locking Managing Locks](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d385503460f94a12376/html5/thumbnails/13.jpg)
Types of Locks
Basic Locks
Shared
Exclusive
Special Situation Locks
Intent
Update
Schema
Bulk update
![Page 14: Module 15: Managing Transactions and Locks. Overview Introduction to Transactions and Locks Managing Transactions SQL Server Locking Managing Locks](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d385503460f94a12376/html5/thumbnails/14.jpg)
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
![Page 15: Module 15: Managing Transactions and Locks. Overview Introduction to Transactions and Locks Managing Transactions SQL Server Locking Managing Locks](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d385503460f94a12376/html5/thumbnails/15.jpg)
Session-Level Locking Options
Dynamic Locking Architecture
Table-Level Locking Options
Deadlocks
Displaying Locking Information
Managing Locks
![Page 16: Module 15: Managing Transactions and Locks. Overview Introduction to Transactions and Locks Managing Transactions SQL Server Locking Managing Locks](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d385503460f94a12376/html5/thumbnails/16.jpg)
Session-Level Locking Options
Transaction Isolation Level
READ COMMITTED (DEFAULT) READ UNCOMMITTED REPEATABLE READ SERIALIZABLE
Locking Timeout
Limits time waiting for a locked resource Use SET LOCK_TIMEOUT
![Page 17: Module 15: Managing Transactions and Locks. Overview Introduction to Transactions and Locks Managing Transactions SQL Server Locking Managing Locks](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d385503460f94a12376/html5/thumbnails/17.jpg)
Dynamic Locking Architecture
DynamicLocking
TablePageRow
Cost
GranularityLocking CostConcurrency Cost
![Page 18: Module 15: Managing Transactions and Locks. Overview Introduction to Transactions and Locks Managing Transactions SQL Server Locking Managing Locks](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d385503460f94a12376/html5/thumbnails/18.jpg)
Table-Level Locking Options
Use with Caution
Can Specify One or More Locking Options for a Table
Use optimizer_hints Portion of FROM Clause inSELECT or UPDATE Statement
Overrides Session-Level Locking Options
![Page 19: Module 15: Managing Transactions and Locks. Overview Introduction to Transactions and Locks Managing Transactions SQL Server Locking Managing Locks](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d385503460f94a12376/html5/thumbnails/19.jpg)
Deadlocks
How SQL Server Ends A Deadlock
How to Minimize Deadlocks
How to Customize the Lock Time-Out Setting
![Page 20: Module 15: Managing Transactions and Locks. Overview Introduction to Transactions and Locks Managing Transactions SQL Server Locking Managing Locks](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d385503460f94a12376/html5/thumbnails/20.jpg)
Displaying Locking Information
Current Activity Window sp_lock System Stored Procedure SQL Profiler Windows 2000 System Monitor Additional Information
![Page 21: Module 15: Managing Transactions and Locks. Overview Introduction to Transactions and Locks Managing Transactions SQL Server Locking Managing Locks](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d385503460f94a12376/html5/thumbnails/21.jpg)
Recommended Practices
Design Transactions to Minimize DeadlocksDesign Transactions to Minimize Deadlocks
Use SQL Server Defaults for LockingUse SQL Server Defaults for Locking
Keep Transactions ShortKeep Transactions Short
Be Careful When You Use Locking OptionsBe Careful When You Use Locking Options
![Page 22: Module 15: Managing Transactions and Locks. Overview Introduction to Transactions and Locks Managing Transactions SQL Server Locking Managing Locks](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d385503460f94a12376/html5/thumbnails/22.jpg)
Review
Introduction to Transactions and Locks
Managing Transactions
SQL Server Locking
Managing Locks