Databases - Transactions II. (GF Royle, N Spadaccini ) Databases - Transactions II 1 / 22

Similar documents
Databases - Transactions

L i (A) = transaction T i acquires lock for element A. U i (A) = transaction T i releases lock for element A

What are Transactions? Transaction Management: Introduction (Chap. 16) Major Example: the web app. Concurrent Execution. Web app in execution (CS636)

Transaction Management: Introduction (Chap. 16)

Intro to Transaction Management

CSE 344 MARCH 9 TH TRANSACTIONS

Introduction to Data Management CSE 414

Database Systems CSE 414

TRANSACTION MANAGEMENT

Database Systems. Announcement

Overview of Transaction Management

Overview. Introduction to Transaction Management ACID. Transactions

CSE 344 MARCH 25 TH ISOLATION

Introduction to Data Management CSE 344

Transaction Management and Concurrency Control. Chapter 16, 17

Database Systems CSE 414

Lock-based Concurrency Control

Phantom Problem. Phantom Problem. Phantom Problem. Phantom Problem R1(X1),R1(X2),W2(X3),R1(X1),R1(X2),R1(X3) R1(X1),R1(X2),W2(X3),R1(X1),R1(X2),R1(X3)

Conflict Equivalent. Conflict Serializability. Example 1. Precedence Graph Test Every conflict serializable schedule is serializable

Goal of Concurrency Control. Concurrency Control. Example. Solution 1. Solution 2. Solution 3

Transaction Management Overview. Transactions. Concurrency in a DBMS. Chapter 16

Transaction Management Overview

Introduction to Data Management CSE 344

Concurrency Control & Recovery

Concurrency Control & Recovery

CSE 444: Database Internals. Lectures Transactions

Database Management System

Lock Granularity and Consistency Levels (Lecture 7, cs262a) Ali Ghodsi and Ion Stoica, UC Berkeley February 7, 2018

Transactions. Kathleen Durant PhD Northeastern University CS3200 Lesson 9

CSC 261/461 Database Systems Lecture 21 and 22. Spring 2017 MW 3:25 pm 4:40 pm January 18 May 3 Dewey 1101

CSE 190D Database System Implementation

Database Applications (15-415)

Transactions. Transaction. Execution of a user program in a DBMS.

Transaction Processing: Concurrency Control. Announcements (April 26) Transactions. CPS 216 Advanced Database Systems

SQL: Transactions. Announcements (October 2) Transactions. CPS 116 Introduction to Database Systems. Project milestone #1 due in 1½ weeks

Transaction Processing: Concurrency Control ACID. Transaction in SQL. CPS 216 Advanced Database Systems. (Implicit beginning of transaction)

Implementing Isolation

Transaction Management & Concurrency Control. CS 377: Database Systems

Intro to Transactions

h p:// Authors: Tomáš Skopal, Irena Holubová Lecturer: Mar n Svoboda, mar

Concurrency Control 1

Concurrency Control! Snapshot isolation" q How to ensure serializability and recoverability? " q Lock-Based Protocols" q Other Protocols"

SQL: Transactions. Introduction to Databases CompSci 316 Fall 2017

Transactions & Update Correctness. April 11, 2018

Concurrency control 12/1/17

Introduction TRANSACTIONS & CONCURRENCY CONTROL. Transactions. Concurrency

Chapter 22. Transaction Management

Transaction Management: Concurrency Control

CSE 444: Database Internals. Lectures 13 Transaction Schedules

Weak Levels of Consistency

Transactions Processing (i)

Introduction to Data Management. Lecture #24 (Transactions)

TRANSACTION PROCESSING CONCEPTS

Transaction Processing Concepts and Theory. Truong Tuan Anh CSE-HCMUT

Chapter 7 (Cont.) Transaction Management and Concurrency Control

Transactions and Isolation

Database Management System

Introduction to Data Management CSE 344

Module 15: Managing Transactions and Locks

UNIT-IV TRANSACTION PROCESSING CONCEPTS

Lectures 8 & 9. Lectures 7 & 8: Transactions

Database Principles: Fundamentals of Design, Implementation, and Management Tenth Edition. Chapter 13 Managing Transactions and Concurrency

Lecture 21. Lecture 21: Concurrency & Locking

Introduction to Transaction Processing Concepts and Theory

Advances in Data Management Transaction Management A.Poulovassilis

CS 370 Concurrency worksheet. T1:R(X); T2:W(Y); T3:R(X); T2:R(X); T2:R(Z); T2:Commit; T3:W(X); T3:Commit; T1:W(Y); Commit

doc. RNDr. Tomáš Skopal, Ph.D.

Transaction Management Exercises KEY

Introduction to Data Management. Lecture #18 (Transactions)

Slides Courtesy of R. Ramakrishnan and J. Gehrke 2. v Concurrent execution of queries for improved performance.

Introduction to Data Management. Lecture #26 (Transactions, cont.)

Transaction Management. Pearson Education Limited 1995, 2005

How Oracle Does It. No Read Locks

Transaction Processing. Introduction to Databases CompSci 316 Fall 2018

CMPT 354: Database System I. Lecture 11. Transaction Management

CSC 261/461 Database Systems Lecture 24

TRANSACTION PROPERTIES

CS352 Lecture - Concurrency

COURSE 1. Database Management Systems

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Seminar 3. Transactions. Concurrency Management in MS SQL Server

Concurrency Control. [R&G] Chapter 17 CS432 1

Introduction to Databases, Fall 2005 IT University of Copenhagen. Lecture 10: Transaction processing. November 14, Lecturer: Rasmus Pagh

Database transactions

Concurrency Control. Conflict Serializable Schedules. Example. Chapter 17

CSE 530A ACID. Washington University Fall 2013

Security Mechanisms I. Key Slide. Key Slide. Security Mechanisms III. Security Mechanisms II

Introducing Transactions

Concurrency Control. Transaction Management. Lost Update Problem. Need for Concurrency Control. Concurrency control

Chapter 20 Introduction to Transaction Processing Concepts and Theory

Information Systems (Informationssysteme)

CS 5300 module6. Problem #1 (10 Points) a) Consider the three transactions T1, T2, and T3, and the schedules S1 and S2.

transaction - (another def) - the execution of a program that accesses or changes the contents of the database

JDBC, Transactions. Niklas Fors JDBC 1 / 38

DATABASE TRANSACTIONS. CS121: Relational Databases Fall 2017 Lecture 25

Including Aborts in Serializability. Conflict Serializable Schedules. Recall Conflicts. Conflict Equivalent

Transaction Management

Copyright 2007 Ramez Elmasri and Shamkant B. Navathe. Slide 17-1

Database Management Systems CSEP 544. Lecture 9: Transactions and Recovery

Transactions. 1. Transactions. Goals for this lecture. Today s Lecture

Transcription:

Databases - Transactions II (GF Royle, N Spadaccini 2006-2010) Databases - Transactions II 1 / 22

This lecture This lecture discusses how a DBMS schedules interleaved transactions to avoid the anomalies described in the lecture Transactions I. (GF Royle, N Spadaccini 2006-2010) Databases - Transactions II 2 / 22

Locking Schedules A schedule (of interleaved statements) is called serializable if its effect on any consistent database instance is equivalent to running the transactions in some serial order. A schedule is called recoverable if a transaction T 1 that reads values changed by T 2 only commits after T 2 commits. The job of the DBMS is to ensure that the only allowed schedules are serializable and recoverable. (GF Royle, N Spadaccini 2006-2010) Databases - Transactions II 3 / 22

Locking Serializable... This schedule is serializable because if both transactions commit as shown, then the effect is the same as running T 1 and then T 2. T 1 Time T 2 R(A) W(A) R(C) W(C) Commit R(A) W(A) R(B) W(B) Commit (GF Royle, N Spadaccini 2006-2010) Databases - Transactions II 4 / 22

Locking... but not recoverable This schedule is not recoverable because if T 1 aborts, then the earlier statements need to be undone; however T 2 has already used the value of A and committed. T 1 Time T 2 R(A) W(A) R(C) W(C) Abort R(A) W(A) R(B) W(B) Commit (GF Royle, N Spadaccini 2006-2010) Databases - Transactions II 5 / 22

Locking Locking The main way in which a DBMS ensures that only serializable, recoverable schedules are allowed is through locking protocols. A lock is a flag, or indicator, that can be attached to a database object indicating that it is in use by a transaction; a second transaction wishing to use the same DB object may have to wait until the first transaction has finished it. A locking protocol is a set of rules that determine what types of lock to use in particular situations. (GF Royle, N Spadaccini 2006-2010) Databases - Transactions II 6 / 22

Locking Strict Two-Phase Locking The most widely used locking protocol is Strict Two-Phase Locking (Strict 2PL) which uses two rules. A transaction that wishes to read an object first requests a shared lock on that object, while a transaction that wishes to modify an object first requests an exclusive lock on that object. All locks held by a transaction are released when the transaction completes (commits or aborts). (GF Royle, N Spadaccini 2006-2010) Databases - Transactions II 7 / 22

Locking Altering a schedule Now consider the non-recoverable schedule from above with Strict 2PL. T 1 Time T 2 R(A) W(A) R(C) W(C) Abort R(A) W(A) R(B) W(B) Commit T 1 Time T 2 S(A) R(A) X(A) W(A) S(A) blocks (GF Royle, N Spadaccini 2006-2010) Databases - Transactions II 8 / 22

Locking Blocking When transaction T 2 requests a shared lock (to read A) the DBMS cannot grant that until T 1 releases its exclusive lock. Therefore T 2 is blocked until T 1 completes either by committing or aborting. Thus the non-recoverable schedule is disallowed under the operation of Strict 2PL. (GF Royle, N Spadaccini 2006-2010) Databases - Transactions II 9 / 22

Locking Deadlock A transaction T 1 is blocked when it is waiting for another transaction, say T 2 to release a lock. But what happens if T 2 is also blocked because it is waiting for T 1 to release a lock? In this situation, the two (or more) transactions are deadlocked and without DBMS intervention they would both remain blocked indefinitely. The DBMS must therefore determine when a transaction is deadlocked and abort it. Detecting deadlock can be done by maintaining an explicit list of blocked transactions and who they are waiting for and then detecting cycles in that list. Alternatively a simpler mechanism is simply to abort any transaction that has been blocked for too long, assuming that it is deadlocked. (GF Royle, N Spadaccini 2006-2010) Databases - Transactions II 10 / 22

Locking Performance Because it causes some transactions to be blocked, any locking protocol will cause some degradation in performance. If there are only a few transactions active, then adding another one will cause the total throughput to increase as interleaving allows it to use CPU idle time. As more transactions are added, some blocking occurs and so the increase in throughput is less. Eventually, each additional transaction conflicts with so many others that the total throughput is reduced. At this point the system is said to be thrashing. (GF Royle, N Spadaccini 2006-2010) Databases - Transactions II 11 / 22

Locking Diminishing returns Throughput Thrashing Active Transactions (GF Royle, N Spadaccini 2006-2010) Databases - Transactions II 12 / 22

Locking Granularity Locking performance can be substantially affected by the granularity of the locking protocol. For example, suppose that a transaction is going to modify a few rows in a large table a coarse-grained locking protocol would lock the entire table, while a fine-grained locking protocol would lock just the individual rows that will be affected. (GF Royle, N Spadaccini 2006-2010) Databases - Transactions II 13 / 22

Transactions in MySQL In MySQL In order to use transactions in MySQL it is necessary to use a transaction-safe database engine at the current time (2009) that means using either InnoDB or BDB (Berkeley DB). The default database engine, MyISAM does not support transactions in effect it behaves as though each statement were a single-statement transaction. In particular, with MyISAM it is not possible to guarantee the atomicity of more than a single statement. (GF Royle, N Spadaccini 2006-2010) Databases - Transactions II 14 / 22

Transactions in MySQL InnoDB The InnoDB storage engine does support transactions. A transaction can be initiated by a user with the statement START TRANSACTION; All statements after that will be deemed to form part of the same transaction until one of the statements COMMIT; or ROLLBACK; occurs. (GF Royle, N Spadaccini 2006-2010) Databases - Transactions II 15 / 22

Transactions in MySQL Example Suppose students are forming project groups and being entered into the following database table. CREATE TABLE groups( id INT, snum INT, UNIQUE(sNum)) Engine = InnoDB; The UNIQUE keyword will ensure that no student accidentally ends up allocated to more than one group. (GF Royle, N Spadaccini 2006-2010) Databases - Transactions II 16 / 22

Transactions in MySQL Initial data Suppose that the first group has been entered: SELECT * FROM groups; +------+------+ id snum +------+------+ 1 1537 1 1433 +------+------+ and that the second proposed group comprises students 1010 and 1537; of course 1537 is already in a group and so adding this group should fail. (GF Royle, N Spadaccini 2006-2010) Databases - Transactions II 17 / 22

Transactions in MySQL Use a transaction START TRANSACTION; INSERT INTO groups VALUES(2,1010); INSERT INTO groups VALUES(2,1537); ERROR 1062 (23000): Duplicate entry 1537 for key 1 The first student was correctly added, but the second violates the key constraint now the entire transaction can be aborted by issuing the ROLLBACK statement. ROLLBACK; SELECT * FROM groups; +------+------+ id snum +------+------+ 1 1537 1 1433 +------+------+ (GF Royle, N Spadaccini 2006-2010) Databases - Transactions II 18 / 22

Transactions in MySQL Rolling Back In addition to explicitly issuing a ROLLBACK statement, this will happen automatically if the client becomes disconnected before the transaction is committed. Therefore critical transactions will never get left half-done. There is also the facility to partially roll-back a transaction to an intermediate checkpoint that is declared inside a transaction. SAVEPOINT doneone; If something goes wrong in the next few statements, the user can ROLLBACK TO SAVEPOINT doneone; rather than rolling back the entire transaction. (GF Royle, N Spadaccini 2006-2010) Databases - Transactions II 19 / 22

Transactions in MySQL Isolation Levels MySQL permits the user to choose how isolated they wish each transaction to be by choosing between READ UNCOMMITTED READ COMMITTED REPEATABLE READ SERIALIZABLE The user can set the isolation level on a per-session or even per-transaction basis, using statements such as: SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED (GF Royle, N Spadaccini 2006-2010) Databases - Transactions II 20 / 22

Transactions in MySQL Isolation levels The READ UNCOMMITTED and READ COMMITTED isolation levels determine when a transaction will see a value that has been changed by another transaction either immediately or only after the other transaction has committed. The default isolation level of REPEATABLE READ guarantees that a transaction will get the same results each time it issues the same SELECT statement. (MySQL also guarantees that REPEATABLE READ prevents phantoms.) The top level of isolation, SERIALIZABLE is essentially like REPEATABLE READ with minor differences to exactly which statements the guarantee applies to. (GF Royle, N Spadaccini 2006-2010) Databases - Transactions II 21 / 22

Transactions in MySQL Isolation Level Summary This table summarizes the anomalies that can or cannot arise at the different isolation levels. Level Dirty Read Unrepeatable Read Phantom READ UNCOMITTED Yes Yes Yes READ COMITTED No Yes Yes REPEATABLE READ No No No 1 SERIALIZABLE No No No 1 MySQL-specific (GF Royle, N Spadaccini 2006-2010) Databases - Transactions II 22 / 22