Module 11: Managing Transactions and Locks

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.
Transaction Processing. Objectives After completing this lesson, you should be able to do the following: –Define transactions effectively for an application.
What is a Transaction? A transaction is a logical logic of work A transaction may have one of two outcomes –When a transaction completes successfully,
Allowing Multi-user Access Grant – GRANT ON TO |WITH GRANT OPTION | –GRANT TO | WITH ADMIN OPTION| – can be PUBLIC or a role – can be ALL Revoke – REVOKE.
1 Data Concurrency David Konopnicki 1997 Revised by Mordo Shalom 2004.
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.
Transaction Management and Concurrency Control
Transaction Management and Concurrency Control
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Transaction Management and Concurrency Control.
1 Transaction Management Database recovery Concurrency control.
Security and Transaction Management Pertemuan 8 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
9 Chapter 9 Transaction Management and Concurrency Control Hachim Haddouti.
Database Administration Part 1 Chapter Six CSCI260 Database Applications.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Transaction Management and Concurrency Control.
Managing Concurrency in Web Applications. DBI 2007 HUJI-CS 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses.
Transaction Management and Concurrency Control
Chapter 9 Overview  Reasons to monitor SQL Server  Performance Monitoring and Tuning  Tools for Monitoring SQL Server  Common Monitoring and Tuning.
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
Transactions and Exception Handling Eric Allsop SQLBits 6 th October 2007.
Allowing Multi-user Access Grant – GRANT ON TO – |WITH GRANT OPTION / WITH ADMIN OPTION| – can be PUBLIC or a role – can be all Revoke – REVOKE ON FROM.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 10 Transaction Management.
Oracle Locking Michael Messina Principal Database Analyst Indiana University.
1cs Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect.
Concurrency and Transaction Processing. Concurrency models 1. Pessimistic –avoids conflicts by acquiring locks on data that is being read, so no other.
1 IT420: Database Management and Organization Session Control Managing Multi-user Databases 24 March 2006 Adina Crăiniceanu
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 136 Database Systems I SQL Modifications and Transactions.
11/7/2012ISC329 Isabelle Bichindaritz1 Transaction Management & Concurrency Control.
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
SQLintersection Understanding Transaction Isolation Levels Randy Knight Wednesday, 3:45-5:00.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L12_JDBC_MySQL 1 Transations.
Transactions, Roles & Privileges Oracle and ANSI Standard SQL Lecture 11.
1 Advanced Database Concepts Transaction Management and Concurrency Control.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Transaction Management and Concurrency Control.
©Bob Godfrey, 2002, 2005 Lecture 17: Transaction Integrity and Concurrency BSA206 Database Management Systems.
3 Database Systems: Design, Implementation, and Management CHAPTER 9 Transaction Management and Concurrency Control.
Module 14: Managing Transactions and Locks. Overview Introducing Transactions and Locks Managing Transactions Understanding SQL Server Locking Architecture.
18 September 2008CIS 340 # 1 Last Covered (almost)(almost) Variety of middleware mechanisms Gain? Enable n-tier architectures while not necessarily using.
In this session, you will learn to: Implement triggers Implement transactions Objectives.
Delete Data Database Administration Fundamentals LESSON 3.4.
Chapter 13 Managing Transactions and Concurrency Database Principles: Fundamentals of Design, Implementation, and Management Tenth Edition.
Locks, Blocks & Isolation Oh My!. About Me Keith Tate Data Professional for over 14 Years MCITP in both DBA and Dev tracks
Oracle 11g: SQL Chapter 5 Data Manipulation and Transaction Control.
Database Transactions  Transaction Management and Concurrency Control.
Transaction Management and Concurrency Control
LAB: Web-scale Data Management on a Cloud
Isolation Levels Understanding Transaction Temper Tantrums
Transaction Properties
On transactions, and Atomic Operations
Batches, Transactions, & Errors
Transactions, Locking and Query Optimisation
Chapter 10 Transaction Management and Concurrency Control
Understanding Transaction Isolation Levels
On transactions, and Atomic Operations
Batches, Transactions, & Errors
Introduction of Week 13 Return assignment 11-1 and 3-1-5
Objectives Define and describe transactions
Transactions and Concurrency
Database Administration
Isolation Levels Understanding Transaction Temper Tantrums
DB Concurrency ITEC 340 Database I Dr. Ian Barland
Presentation transcript:

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?

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

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

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

Lesson 2: Managing Transactions Autocommit Transactions Explicit Transactions Implicit Transactions Transaction Recovery Considerations for Using Transactions Restricted Statements

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

Explicit Transactions BEGIN TRANSACTION COMMIT TRANSACTION ROLLBACK TRANSACTION SAVE TRANSACTION Transaction log BEGIN TRANSACTION fund_transfer EXEC debit_checking '100', 'account1' EXEC credit_savings '100', 'account2' COMMIT TRANSACTION

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 ON ALTER DATABASE CREATE DELETE DROP FETCH GRANT INSERT OPEN REVOKE SELECT TRUNCATE TABLE UPDATE

Transaction Recovery Transaction Recovery Action Required Checkpoint 1 None 2 Roll forward 3 Roll back 4 Roll forward 5 Roll back Checkpoint System Failure

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

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

Lesson 3: Understanding SQL Server Locking Architecture What Concurrency Problems Are Prevented by Locks? Lockable Resources Types of Locks Lock Compatibility

What Concurrency Problems Are Prevented by Locks? Lost updates Uncommitted dependencies (dirty read) Inconsistent analysis (nonrepeatable read) Phantom reads

Lockable Resources Item Description RID Row identifier KEY Row lock within an index PAGE Data page or index page EXTENT Group of pages TABLE Entire table HOBT A heap or B-tree FILE A database file APPLICATION An application-specified resource METADATA Metadata locks ALLOCATION_UNIT An allocation unit DATABASE Entire database

Types of Locks Basic locks Shared Exclusive Special situation locks Intent Update Schema Bulk update

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

Lesson 4: Managing Locks Session-Level Locking Options Dynamic Locking What Are Deadlocks? Methods to View Locking Information Best Practices

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

Dynamic Locking Dynamic Locking Cost Row Page Table Locking Cost Concurrency Cost Granularity

What Are Deadlocks? How SQL server ends a deadlock How to minimize deadlocks How to customize the lock time-out setting Transaction A Transaction B UPDATE Supplier UPDATE Part UPDATE Part UPDATE Supplier Supplier Part A B A

Methods to View Locking Information Activity Monitor window sys.dm_tran_locks Dynamic Management View EnumLocks method SQL Server Profiler Windows 2003 System Monitor

Best Practices ü Keep transactions short ü Design transactions to minimize deadlocks ü Check for error 1205 ü Set LOCK_TIMEOUT with care ü Monitor locking information

Lab: Managing Transactions and Locks Exercise 1: Using Transactions Exercise 2: Managing Locks