module 11: managing transactions and locks
DESCRIPTION
Module 11: Managing Transactions and Locks. Overview. Overview of Transactions and Locks Managing Transactions Understanding SQL Server Locking Architecture Managing Locks. Lesson 1: Overview of Transactions and Locks. What Are Transactions? What Are Locks? What Is Concurrency Control?. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Module 11: Managing Transactions and Locks](https://reader036.vdocument.in/reader036/viewer/2022062422/56813427550346895d9b1421/html5/thumbnails/1.jpg)
Module 11: Managing Transactions
and Locks
![Page 2: Module 11: Managing Transactions and Locks](https://reader036.vdocument.in/reader036/viewer/2022062422/56813427550346895d9b1421/html5/thumbnails/2.jpg)
Overview
Overview of Transactions and Locks
Managing Transactions
Understanding SQL Server Locking Architecture
Managing Locks
![Page 3: Module 11: Managing Transactions and Locks](https://reader036.vdocument.in/reader036/viewer/2022062422/56813427550346895d9b1421/html5/thumbnails/3.jpg)
Lesson 1: Overview of Transactions and Locks
What Are Transactions?
What Are Locks?
What Is Concurrency Control?
![Page 4: Module 11: Managing Transactions and Locks](https://reader036.vdocument.in/reader036/viewer/2022062422/56813427550346895d9b1421/html5/thumbnails/4.jpg)
What Are Transactions?
A transaction is a logical unit of work that displays ACID properties
Atomicity Consistency Isolation Durability
Transactions ensure that multiple data modifications are processed together or not at all
The transaction log ensures that updates are complete and recoverable
Transactions use locks
![Page 5: Module 11: Managing Transactions and Locks](https://reader036.vdocument.in/reader036/viewer/2022062422/56813427550346895d9b1421/html5/thumbnails/5.jpg)
What Are Locks?
Two main types of lock:
Read locks – allow others to read but not write Write locks – stop others reading or writing
Deadlocks can occur
Locks prevent update conflicts
Locking ensures that transactions are serialized Locking is automatic Locks enable concurrent use of data
![Page 6: Module 11: Managing Transactions and Locks](https://reader036.vdocument.in/reader036/viewer/2022062422/56813427550346895d9b1421/html5/thumbnails/6.jpg)
What Is Concurrency Control?
Pessimistic
Locks data when data is read in preparation for an update Other users are blocked until lock is released Use where high contention for data exists
Optimistic
Locks data when an update is performed Error received if data was changed since initial read Use when low contention for data exists
![Page 7: Module 11: Managing Transactions and Locks](https://reader036.vdocument.in/reader036/viewer/2022062422/56813427550346895d9b1421/html5/thumbnails/7.jpg)
Lesson 2: Managing Transactions
Autocommit Transactions
Explicit Transactions
Implicit Transactions
Transaction Recovery
Considerations for Using Transactions
Restricted Statements
![Page 8: Module 11: Managing Transactions and Locks](https://reader036.vdocument.in/reader036/viewer/2022062422/56813427550346895d9b1421/html5/thumbnails/8.jpg)
Autocommit Transactions
Default transaction mode
Every statement is committed or rolled back when it has completed
If it completes successfully – it is committed If it fails – it is rolled back
Compile errors result in a batch not being executed
![Page 9: Module 11: Managing Transactions and Locks](https://reader036.vdocument.in/reader036/viewer/2022062422/56813427550346895d9b1421/html5/thumbnails/9.jpg)
Explicit Transactions
BEGIN TRANSACTION
COMMIT TRANSACTION
ROLLBACK TRANSACTION
SAVE TRANSACTION
Transaction log
BEGIN TRANSACTION fund_transferEXEC debit_checking '100', 'account1'EXEC credit_savings '100', 'account2'
COMMIT TRANSACTION
BEGIN TRANSACTION fund_transferEXEC debit_checking '100', 'account1'EXEC credit_savings '100', 'account2'
COMMIT TRANSACTION
![Page 10: Module 11: Managing Transactions and Locks](https://reader036.vdocument.in/reader036/viewer/2022062422/56813427550346895d9b1421/html5/thumbnails/10.jpg)
Implicit Transactions
Setting implicit transaction mode on
An implicit transaction starts when one of the following statements is executed
Transaction must be explicitly completed with COMMIT or ROLLBACK TRANSACTION
SET IMPLICIT_TRANSACTION ONSET IMPLICIT_TRANSACTION ON
ALTER DATABASE CREATE DELETE DROP FETCH GRANT
INSERT OPEN REVOKE SELECT TRUNCATE TABLE UPDATE
![Page 11: Module 11: Managing Transactions and Locks](https://reader036.vdocument.in/reader036/viewer/2022062422/56813427550346895d9b1421/html5/thumbnails/11.jpg)
Transaction Recovery
Transaction Recovery Action Required
None
Checkpoint System Failure
Roll forward
Roll back
Roll forward
Roll back
11
22
33
44
55
![Page 12: Module 11: Managing Transactions and Locks](https://reader036.vdocument.in/reader036/viewer/2022062422/56813427550346895d9b1421/html5/thumbnails/12.jpg)
Considerations for Using Transactions
Keep transactions as short as possible
Use caution with certain Transact-SQL statements Avoid transactions that require user interaction Do not browse data during a transaction Affect the least rows possible with DML statements Access the least rows possible with SELECT statements
Issues with nested transactions
Allowed, but not recommended Use @@trancount to determine nesting level
![Page 13: Module 11: Managing Transactions and Locks](https://reader036.vdocument.in/reader036/viewer/2022062422/56813427550346895d9b1421/html5/thumbnails/13.jpg)
Restricted Statements
Certain statements may not be included in explicit transactions, such as:
Full-text system stored procedure calls may not be included in explicit transactions
You cannot use the following in implicit or explicit transactions:
sp_dboption System stored procedures that modify master
ALTER DATABASE BACKUP CREATE DATABASE DROP DATABASE
RECONFIGURE RESTORE DATABASE RESTORE UPDATE STATISTICS
![Page 14: Module 11: Managing Transactions and Locks](https://reader036.vdocument.in/reader036/viewer/2022062422/56813427550346895d9b1421/html5/thumbnails/14.jpg)
Lesson 3: Understanding SQL Server Locking Architecture
What Concurrency Problems Are Prevented by Locks?
Lockable Resources
Types of Locks
Lock Compatibility
![Page 15: Module 11: Managing Transactions and Locks](https://reader036.vdocument.in/reader036/viewer/2022062422/56813427550346895d9b1421/html5/thumbnails/15.jpg)
What Concurrency Problems Are Prevented by Locks?
Lost updates
Uncommitted dependencies (dirty read)
Inconsistent analysis (nonrepeatable read)
Phantom reads
![Page 16: Module 11: Managing Transactions and Locks](https://reader036.vdocument.in/reader036/viewer/2022062422/56813427550346895d9b1421/html5/thumbnails/16.jpg)
Lockable Resources
Item DescriptionRID Row identifierKEY Row lock within an indexPAGE Data page or index pageEXTENT Group of pagesTABLE Entire tableHOBT A heap or B-treeFILE A database fileAPPLICATION An application-specified resourceMETADATA Metadata locksALLOCATION_UNIT An allocation unitDATABASE Entire database
![Page 17: Module 11: Managing Transactions and Locks](https://reader036.vdocument.in/reader036/viewer/2022062422/56813427550346895d9b1421/html5/thumbnails/17.jpg)
Types of Locks
Basic locks
Shared Exclusive
Special situation locks
Intent Update Schema Bulk update
![Page 18: Module 11: Managing Transactions and Locks](https://reader036.vdocument.in/reader036/viewer/2022062422/56813427550346895d9b1421/html5/thumbnails/18.jpg)
Lock Compatibility
Some locks are compatible with other locks, and some locks are not
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 19: Module 11: Managing Transactions and Locks](https://reader036.vdocument.in/reader036/viewer/2022062422/56813427550346895d9b1421/html5/thumbnails/19.jpg)
Lesson 4: Managing Locks
Session-Level Locking Options
Dynamic Locking
What Are Deadlocks?
Methods to View Locking Information
Best Practices
![Page 20: Module 11: Managing Transactions and Locks](https://reader036.vdocument.in/reader036/viewer/2022062422/56813427550346895d9b1421/html5/thumbnails/20.jpg)
Session-Level Locking Options
Transaction isolation level
READ UNCOMMITTED READ COMMITTED (default) REPEATABLE READ SERIALIZABLE
Locking timeout
Limits time waiting for a locked resource Use SET LOCK_TIMEOUT
![Page 21: Module 11: Managing Transactions and Locks](https://reader036.vdocument.in/reader036/viewer/2022062422/56813427550346895d9b1421/html5/thumbnails/21.jpg)
Dynamic Locking
DynamicLocking
TablePageRow
Cost
GranularityLocking CostConcurrency CostLocking CostConcurrency Cost
![Page 22: Module 11: Managing Transactions and Locks](https://reader036.vdocument.in/reader036/viewer/2022062422/56813427550346895d9b1421/html5/thumbnails/22.jpg)
What Are Deadlocks?
SupplierSupplier
PartPart
Transaction AUPDATE Supplier
UPDATE Part
Transaction BUPDATE Part
UPDATE Supplier
A BA
How SQL server ends a deadlock
How to minimize deadlocks
How to customize the lock time-out setting
![Page 23: Module 11: Managing Transactions and Locks](https://reader036.vdocument.in/reader036/viewer/2022062422/56813427550346895d9b1421/html5/thumbnails/23.jpg)
Methods to View Locking Information
Activity Monitor window
sys.dm_tran_locks Dynamic Management View
EnumLocks method
SQL Server Profiler
Windows 2003 System Monitor
![Page 24: Module 11: Managing Transactions and Locks](https://reader036.vdocument.in/reader036/viewer/2022062422/56813427550346895d9b1421/html5/thumbnails/24.jpg)
Best Practices
Set LOCK_TIMEOUT with careSet LOCK_TIMEOUT with care
Check for error 1205Check for error 1205
Design transactions to minimize deadlocksDesign transactions to minimize deadlocks
Keep transactions shortKeep transactions short
Monitor locking informationMonitor locking information
![Page 25: Module 11: Managing Transactions and Locks](https://reader036.vdocument.in/reader036/viewer/2022062422/56813427550346895d9b1421/html5/thumbnails/25.jpg)
Lab: Managing Transactions and Locks
Exercise 1: Using Transactions
Exercise 2: Managing Locks