Transaction Management Chapter 11. Class 9: Transaction Management 1

Similar documents
Seminar 3. Transactions. Concurrency Management in MS SQL Server

Module 15: Managing Transactions and Locks

CSE 530A ACID. Washington University Fall 2013

Monitoring and Resolving Lock Conflicts. Copyright 2004, Oracle. All rights reserved.

Weak Levels of Consistency

A transaction is a sequence of one or more processing steps. It refers to database objects such as tables, views, joins and so forth.

Introduction to Transaction Processing Concepts and Theory

Transactions and Isolation

Transactions & Concurrency Control

DATABASE TRANSACTIONS. CS121: Relational Databases Fall 2017 Lecture 25

Part VII Data Protection

Chapter 13. Concurrency Control. In This Chapter. c Concurrency Models c Transactions c Locking c Isolation Levels c Row Versioning

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

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

CHAPTER 3 RECOVERY & CONCURRENCY ADVANCED DATABASE SYSTEMS. Assist. Prof. Dr. Volkan TUNALI

Chapter 20 Introduction to Transaction Processing Concepts and Theory

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

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

Chapter 7 (Cont.) Transaction Management and Concurrency Control

Manipulating Data. Copyright 2004, Oracle. All rights reserved.

Introducing Transactions

Developing SQL Databases (762)

Data Manipulation Language

TRANSACTION MANAGEMENT

How Oracle Does It. No Read Locks

Transaction Management

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)

RNDr. Michal Kopecký, Ph.D. Department of Software Engineering, Faculty of Mathematics and Physics, Charles University in Prague

Táblák tartalmának módosítása. Copyright 2004, Oracle. All rights reserved.

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

Database Systems CSE 414

PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND T-SQL)

Transactions. ACID Properties of Transactions. Atomicity - all or nothing property - Fully performed or not at all

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

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI

Database Application Development Oracle PL/SQL, part 2. CS430/630 Lecture 18b

Introduction to Data Management CSE 344

CMP-3440 Database Systems

Relational Database Management System 2014


SQL: Transactions. Introduction to Databases CompSci 316 Fall 2017

Locking, concurrency, and isolation

Part VIII Transactions, Integrity and Triggers

Database Systems CSE 414

Transactions and Concurrency Control. Dr. Philip Cannata

Block 3 The database language SQL. Block 3 The database language SQL. SQL Control Statements. SQL Control Statements. There are seven sections.

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

CSE 190D Database System Implementation

Index. Symbol function, 391

Transactions and Locking. Rose-Hulman Institute of Technology Curt Clifton

Databases - Transactions

) Intel)(TX)memory):) Transac'onal) Synchroniza'on) Extensions)(TSX))) Transac'ons)

Transaction Concept. Two main issues to deal with:

COSC344 Database Theory and Applications. Lecture 21 Transactions

Vendor: IBM. Exam Code: Exam Name: DB Fundamentals. Version: DEMO

) Intel)(TX)memory):) Transac'onal) Synchroniza'on) Extensions)(TSX))) Transac'ons)

Intro to Transaction Management

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

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

CSE 344 MARCH 25 TH ISOLATION

Chapter 15 : Concurrency Control

2 Copyright 2015 M. E. Kabay. All rights reserved. 4 Copyright 2015 M. E. Kabay. All rights reserved.

CSE 344 MARCH 9 TH TRANSACTIONS

Database Programming with PL/SQL

Database Programming with SQL

پوهنتون کابل پوهنځی كمپيوترساینس

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

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)

Distributed Systems (ICE 601) Transactions & Concurrency Control - Part1

Database Usage (and Construction)

Transactions. Kathleen Durant PhD Northeastern University CS3200 Lesson 9

DATABASE MANAGEMENT SYSTEMS

Problems Caused by Failures

PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND T-SQL)

Implementing Isolation

Oracle 1Z Oracle Database 11g : SQL Fundamentals I. Download Full Version :

Vendor: IBM. Exam Code: C Exam Name: DB Fundamentals. Version: Demo

Integrity Constraints, Triggers, Transactions and Procedures

bobpusateri.com heraflux.com linkedin.com/in/bobpusateri. Solutions Architect

Understanding Isolation Levels and Locking

Advanced Databases. Transactions. Nikolaus Augsten. FB Computerwissenschaften Universität Salzburg

References. Transaction Management. Database Administration and Tuning 2012/2013. Chpt 14 Silberchatz Chpt 16 Raghu

) Intel)(TX)memory):) Transac'onal) Synchroniza'on) Extensions)(TSX))) Transac'ons)

Database Security: Transactions, Access Control, and SQL Injection

Chapter 22. Transaction Management

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

Introduction to Computer Science and Business

Overview. Data Integrity. Three basic types of data integrity. Integrity implementation and enforcement. Database constraints Transaction Trigger

ECE 650 Systems Programming & Engineering. Spring 2018

Advances in Data Management Transaction Management A.Poulovassilis

Announcements. SQL: Part IV. Transactions. Summary of SQL features covered so far. Fine prints. SQL transactions. Reading assignments for this week

Difficult I.Q on Databases, asked to SCTPL level 2 students 2015

Example: Transfer Euro 50 from A to B

CS352 Lecture - Introduction to SQL

CS352 Lecture - Concurrency

Database System Concepts

) Intel)(TX)memory):) Transac'onal) Synchroniza'on) Extensions)(TSX))) Transac'ons)

Transactions: Definition. Concurrent transactions: Problems

Chapter 2. DB2 concepts

Transcription:

Transaction Management Chapter 11 Class 9: Transaction Management 1

The Concurrent Update Problem To prevent errors from being introduced when concurrent updates are attempted, the application logic must incorporate the concept of a transaction. Class 9: Transaction Management 2

Transaction Management Transaction: a discrete series of actions that must be either completely processed or not processed at all. Also called a Unit of Work Predates RDBMSs transactions were formally supported in many hierarchical and network DBMSs Freely sharing data online led to the concurrent update problem Some RDBMSs support nested transactions (e.g. Microsoft SQL Server), while others (e.g. Oracle) do not. Class 9: Transaction Management 3

Transaction Properties (ACID) Atomicity: must remain whole; must completely succeed or completely fail Rollback: reverses all changes made by a transaction Commit: makes changes made by a transaction permanent Consistency: should transform database from one consistent state to another Isolation: should carry out its work independent of any other transaction Durability: changes made by completed transactions should be permanently stored (persistent in OO terminology) Class 9: Transaction Management 4

Transaction Support in SQL ANSI/ISO SQL Standard specifies the syntax and behavior of SQL transactions RDBMS vendors implemented transactions prior to the development of the SQL Standard, so there are a number of departures from the standard. SQL Server and Oracle are presented here as exceptions MySQL conforms closely to the standard Class 9: Transaction Management 5

Standard SQL Transaction Syntax Seven Transaction Control Statements: SET TRANSACTION Sets the properties of the next transaction to be executed START TRANSACTION Sets the properties of a transaction and starts the transaction SET CONSTRAINTS Sets the constraint mode within a current transaction (controls whether constraints are applied immediates or deferred until the commit point) SAVEPOINT Creates a savepoint within a transaction. A savepoint marks a place within the transaction that acts as a stopping point when you roll back a transaction. Class 9: Transaction Management 6

Standard SQL Transaction Syntax Seven Transaction Control Statements (continued): RELEASE SAVEPOINT Releases a savepoint ROLLBACK Terminates a transaction and rolls back any changes to the beginning of the transaction or to a savepoint COMMIT Terminates a transaction and commits all changes to the database SET TRANSACTION is intended for implicitly started transactions, while START TRANSACTION explicitly starts a transaction. Both forms set transaction properties. Class 9: Transaction Management 7

How SQL Transaction Statements Work Together Class 9: Transaction Management 8

Data Phenomena Dirty Reads Can occur when: One transaction modifies data A second transaction sees those modifications before they re actually committed to the database The first transaction rolls back the modifications, returning the database to its original state. However, the second transaction, having read the modified data, might have taken action based on the incorrect data. Class 9: Transaction Management 9

Data Phenomena Nonrepeatable Reads Occur when: One transaction reads data from a table, Another transaction then updates that data The first transaction rereads the data, only to discover that the data has changed. As a result, the first read is not repeatable. Class 9: Transaction Management 10

Data Phenomena Phantom Reads: Can occur when: A transaction reads a table based on some sort of search condition, then A second transaction updates the data in the table Then the first transaction attempts to reread the data, only this time different rows are returned because of how the search condition is defined. Phantom read refers to whether a subsequent read returns the same rows (as opposed to having rows added or removed from the results of the subsequent read) Nonrepeatable read refers to whether the rows returned have different column data than the prior read of the same rows of data. Class 9: Transaction Management 11

Data Phenomena Phantom Reads vs. Nonrepeatable Read Phantom read refers to whether a subsequent read returns the same rows (as opposed to having rows added or removed from the results of the subsequent read) Nonrepeatable read refers to whether the rows returned have different column data than the prior read of the same rows of data. Class 9: Transaction Management 12

Transaction ISOLATION LEVEL Controls Data Phenomena ISOLATION LEVEL Dirty Read Nonrepeatable Read Phantom Read READ UNCOMMITTED Yes Yes Yes READ COMMITTED No Yes Yes REPEATABLE READ No No Yes SERIALIZABLE No No No Tradeoff: the more restrictive the isolation level, the higher the performance penalty Class 9: Transaction Management 13

Examples of SQL Transaction Statements START TRANSACTION ISOLATION LEVEL SERIALIZABLE; UPDATE EMPLOYEE SET SALARY = SALARY * 3.5; -- intending to raise salaries by 3.5% SELECT SUM(SALARY) FROM EMPLOYEE; -- User sees the new sum and recognizes the error. ROLLBACK; -- Rolls back the previous changes and ends trans. Class 9: Transaction Management 14

Examples of SQL Transaction Statements START TRANSACTION ISOLATION LEVEL SERIALIZABLE; UPDATE EMPLOYEE SET SALARY = SALARY *.035; SELECT SUM(SALARY) FROM EMPLOYEE; -- User sees the new sum is correct. COMMIT; -- Commits the changes and ends transaction Class 9: Transaction Management 15

Locking Mechanisms Lock: a control placed in the database to reserve data so that only one database session may update it Locks normally placed by SQL DML (INSERT, UPDATE, DELETE) statements Locks normally released with COMMIT and ROLLBACK Class 9: Transaction Management 16

Locking Mechanisms (Cont.) Lock Granularity: the amount of data held by a lock (see next slide) Lock Escalation: raises locks to higher levels of granularity as the database session invokes more and more locks Many RDBMSs have parameters that control locking behavior Class 9: Transaction Management 17

Lock Granularity Database File Block or Page Row Column Class 9: Transaction Management 18

Locks: A Double Edged Sword Locks solve the concurrent update problem by preventing two sessions from executing conflicting changes to the same data Locks also cause contention as sessions must wait for locked resources to be freed Deadlock: a situation where two or more database sessions have locked some data and each then requests a lock on data that the other has already locked Class 9: Transaction Management 19

The Deadlock Class 9: Transaction Management 20

Transaction Support in Microsoft SQL Server Autocommit Mode (default behavior): each SQL statement is automatically committed as it completes Explicit Mode: Transaction started with BEGIN TRANSACTION Transaction ended successfully with COMMIT Transaction ended unsuccessfully with ROLLBACK Implicit Mode: Mode set with SET IMPLICIT_TRANSACTIONS ON Transaction started implicitly by certain SQL statements Once started, transaction continues until a COMMIT or ROLLBACK Class 9: Transaction Management 21

Transaction Support in Oracle Implicit Mode (default behavior): Start of database session starts a transaction Transaction continues until: COMMIT issued ROLLBACK issued Database session ends DDL statement issued Autocommit Mode: SET AUTOCOMMIT ON SET AUTOCOMMIT OFF Class 9: Transaction Management 22

Logical Locking for Database Applications Database locking does not work when web applications are involved Most web applications are stateless Users can start a transaction and abandon it, leaving resources locked for a long time In this situation, the application layer must handle locking in a logical manner Usually known as application locking Works even in AUTOCOMMIT mode Class 9: Transaction Management 23

Last Update Timestamp Method Table design includes LAST_UPDATE column TIMESTAMP with precision of 4 or more Class 9: Transaction Management 24

Last Update Timestamp Method Implementation Rules (Every Application Must Follow): Any select with an intent to update the table must include the LAST_UPDATE column. Any update of a row in the table must also set the LAST_UPDATE column to the current timestamp. SQL provides pseudo column CURRENT_TIMESTAMP Any update or delete must include a WHERE clause predicate that compares the current value of the LAST_UPDATE column to the last value selected for the LAST_UPDATE column for the same row. Updated row will cause a NOT FOUND condition. Class 9: Transaction Management 25

Last Update Timestamp Method SQL Example: SELECT EMPLOYEE_ID, FIRST_NAME, LAST_NAME, LAST_UPDATE FROM EMPLOYEE INTO :EmpID, :FName, :LName, :LUpdate WHERE EMPLOYEE_ID = 146; UPDATE EMPLOYEE SET LAST_NAME = Chang, LAST_UPDATE = CURRENT_TIMESTAMP WHERE EMPLOYEE_ID = :EmpID predicate on the primary key column AND LAST_UPDATE = :LUpdate; predicate on the last update column Class 9: Transaction Management 26

Versioning Method Uses ROW_VERSION (typically INTEGER) instead of LAST_UPDATE (TIMESTAMP): SELECT EMPLOYEE_ID, FIRST_NAME, LAST_NAME, ROW_VERSION FROM EMPLOYEE INTO :EmpID, :FName, :LName, :RowVersion WHERE EMPLOYEE_ID = 146; UPDATE EMPLOYEE SET LAST_NAME = Chang, ROW_VERSION = ROW_VERSION + 1 WHERE EMPLOYEE_ID = :EmpID AND ROW_VERSION = :RowVersion; Class 9: Transaction Management 27

Session ID Method Relies on a unique session ID Value provided by the application or database server Table includes a column where Session ID can be written Class 9: Transaction Management 28

Session ID Method Every Application Must Follow These Rules: Any update to the table must check (with a WHERE clause predicate) to be sure that the LOCK_SESSION_ID column is either null or contains the session number of the current database session. Any database session (connection) that intends to update the table must update the row first to place the session ID in the LOCK_SESSION_ID column. Even this update must check the column before updating it. Class 9: Transaction Management 29

Session ID Method Rules (Continued): The row data may be read (with the intent to apply updates) only after the LOCK_SESSION_ID update has been applied. The update of the business data in the table row must also clear the Session ID column Logically releasing the application lock) by setting the column value to NULL. This update must also include a WHERE clause predicate that compares the current value of the LOCK_SESSION_ID column to the current session ID. If row not locked by current session, NOT FOUND results. Class 9: Transaction Management 30

Session ID Method Example UPDATE EMPLOYEE SET LOCK_SESSION_ID = @@SPID WHERE EMPLOYEE_ID = 146 AND LOCK_SESSION_ID IS NULL; -- row not found if already locked SELECT EMPLOYEE_ID, FIRST_NAME, LAST_NAME FROM EMPLOYEE INTO @EmpID, @FName, @LName WHERE EMPLOYEE_ID = 146 AND LOCK_SESSION_ID = @@SPID; -- row not found if no session lock Class 9: Transaction Management 31

Session ID Method Example UPDATE EMPLOYEE SET LAST_NAME = Chang, LOCK_SESSION_ID = null -- clears session lock WHERE EMPLOYEE_ID = @EmpID -- predicate on the primary key column AND LAST_UPDATE = @@SPID; -- not found if no session lock Class 9: Transaction Management 32