Module 15: Managing Transactions and Locks Overview Introduction - - PowerPoint PPT Presentation

module 15 managing transactions and locks overview
SMART_READER_LITE
LIVE PREVIEW

Module 15: Managing Transactions and Locks Overview Introduction - - PowerPoint PPT Presentation

Module 15: Managing Transactions and Locks Overview Introduction to Transactions and Locks Managing Transactions SQL Server Locking Managing Locks Introduction to Transactions and Locks Transactions Ensure That Multiple Data


slide-1
SLIDE 1

Module 15: Managing Transactions and Locks

slide-2
SLIDE 2

Overview

 Introduction to Transactions and Locks  Managing Transactions  SQL Server Locking  Managing Locks

slide-3
SLIDE 3

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

slide-4
SLIDE 4

 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

slide-5
SLIDE 5

Multimedia Presentation: SQL Server Transactions

slide-6
SLIDE 6

Transaction Recovery and Checkpoints

Transaction Recovery Action Required

None

Checkpoint System Failure 1 1 2 2 3 3 4 4 5 5

Roll forward Roll back Roll forward Roll back

slide-7
SLIDE 7

Transaction Recovery and Checkpoints

Time (and place in log) Database Transaction Log Transaction Log

INSERT … DELETE … UPDATE … … INSERT … DELETE … UPDATE … … INSERT … DELETE … UPDATE … … INSERT … DELETE … UPDATE … … INSERT … DELETE … UPDATE … …

CHECKPOINT CRASH!!!

COMMIT COMMIT COMMIT

Recovery Needed? NONE Recovery Needed? ROLL FORWARD Recovery Needed? ROLL BACK Recovery Needed? ROLL FORWARD Recovery Needed? ROLL BACK ZOT!

slide-8
SLIDE 8

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

slide-9
SLIDE 9

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 ON

slide-10
SLIDE 10

 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

slide-11
SLIDE 11

 SQL Server Locking

 Concurrency Problems Prevented by Locks  Lockable Resources  Types of Locks  Lock Compatibility

slide-12
SLIDE 12

Concurrency Problems Prevented by Locks

 Lost Update  Uncommitted Dependency (Dirty Read)  Inconsistent Analysis (Nonrepeatable Read)  Phantoms Reads

slide-13
SLIDE 13

Lockable Resources

Item Item Description Description 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

slide-14
SLIDE 14

Types of Locks

 Basic Locks

 Shared  Exclusive

 Special Situation Locks

 Intent  Update  Schema  Bulk update

slide-15
SLIDE 15

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

slide-16
SLIDE 16

 Session-Level Locking Options  Dynamic Locking Architecture  Table-Level Locking Options  Deadlocks  Displaying Locking Information

 Managing Locks

slide-17
SLIDE 17

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

slide-18
SLIDE 18

Dynamic Locking Architecture

Dynamic Locking Table Page Row Cost Granularity

Locking Cost Concurrency Cost

slide-19
SLIDE 19

Table-Level Locking Options

 Use with Caution  Can Specify One or More Locking Options for a Table  Use optimizer_hints Portion of FROM Clause in

SELECT or UPDATE Statement

 Overrides Session-Level Locking Options

slide-20
SLIDE 20

Deadlocks

 How SQL Server Ends A Deadlock  How to Minimize Deadlocks  How to Customize the Lock Time-Out Setting

slide-21
SLIDE 21

Displaying Locking Information

 Current Activity Window  sp_lock System Stored Procedure  SQL Profiler  Windows 2000 System Monitor  Additional Information

slide-22
SLIDE 22

Recommended Practices

Design Transactions to Minimize Deadlocks Use SQL Server Defaults for Locking Keep Transactions Short Be Careful When You Use Locking Options

slide-23
SLIDE 23

Review

 Introduction to Transactions and Locks  Managing Transactions  SQL Server Locking  Managing Locks