Transaction Management Reading: CB, Ch. 22. Dept of Computing Science, University of Aberdeen2 In this lecture you will learn the problems of concurrency.

Slides:



Advertisements
Similar presentations
Database System Concepts 5 th Ed. © Silberschatz, Korth and Sudarshan, 2005 See for conditions on re-usewww.db-book.com Chapter 16 : Concurrency.
Advertisements

Concurrency control 1. 2 Introduction concurrency more than one transaction have access to data simultaneously part of transaction processing.
1 Term 2, 2004, Lecture 6, TransactionsMarian Ursu, Department of Computing, Goldsmiths College Transactions 3.
Access Control & Views Reading: C&B, Chap 7. Dept of Computing Science, University of Aberdeen2 In this lecture you will learn the principles of object.
Lecture plan Transaction processing Concurrency control
MSc IT UFCE8K-15-M Data Management Prakash Chatterjee Room 2Q18
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 16.
Concurrency Control WXES 2103 Database. Content Concurrency Problems Concurrency Control Concurrency Control Approaches.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 16.
Chapter 16 Concurrency. Topics in this Chapter Three Concurrency Problems Locking Deadlock Serializability Isolation Levels Intent Locking Dropping ACID.
1 Lecture 11: Transactions: Concurrency. 2 Overview Transactions Concurrency Control Locking Transactions in SQL.
TRANSACTION PROCESSING SYSTEM ROHIT KHOKHER. TRANSACTION RECOVERY TRANSACTION RECOVERY TRANSACTION STATES SERIALIZABILITY CONFLICT SERIALIZABILITY VIEW.
CSC271 Database Systems Lecture # 32.
Transactions (Chapter ). What is it? Transaction - a logical unit of database processing Motivation - want consistent change of state in data Transactions.
Lock-Based Concurrency Control
Lecture 11 Recoverability. 2 Serializability identifies schedules that maintain database consistency, assuming no transaction fails. Could also examine.
CMPT 401 Summer 2007 Dr. Alexandra Fedorova Lecture X: Transactions.
Database Systems, 8 th Edition Concurrency Control with Time Stamping Methods Assigns global unique time stamp to each transaction Produces explicit.
CMPT Dr. Alexandra Fedorova Lecture X: Transactions.
Transaction Management and Concurrency Control
10 1 Chapter 10 Transaction Management and Concurrency Control Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
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.
Chapter 17: Transaction Management
Chapter 8 : Transaction Management. u Function and importance of transactions. u Properties of transactions. u Concurrency Control – Meaning of serializability.
Transaction Management
1 Minggu 8, Pertemuan 15 Transaction Management Matakuliah: T0206-Sistem Basisdata Tahun: 2005 Versi: 1.0/0.0.
9 Chapter 9 Transaction Management and Concurrency Control Hachim Haddouti.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Transaction Management and Concurrency Control.
Transaction Management WXES 2103 Database. Content What is transaction Transaction properties Transaction management with SQL Transaction log DBMS Transaction.
Transaction Management and Concurrency Control
Transaction. A transaction is an event which occurs on the database. Generally a transaction reads a value from the database or writes a value to the.
Transaction Management Chapter 9. What is a Transaction? A logical unit of work on a database A logical unit of work on a database An entire program An.
Managing Transaction and Lock Vu Tuyet Trinh Hanoi University of Technology 1.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 10 Transaction Management.
Chapterb19 Transaction Management Transaction: An action, or series of actions, carried out by a single user or application program, which reads or updates.
Databases Illuminated
1 Chapter 20 Transaction Management Transparencies Last Updated: 17 th March 2011 By M. Arief
Ch 10: Transaction Management and Concurrent Control.
Transactions CPSC 356 Database Ellen Walker Hiram College (Includes figures from Database Systems by Connolly & Begg, © Addison Wesley 2002)
Concurrency Control in Database Operating Systems.
11/7/2012ISC329 Isabelle Bichindaritz1 Transaction Management & Concurrency Control.
Transactions and Locks A Quick Reference and Summary BIT 275.
Sekolah Tinggi Ilmu Statistik (STIS) 1 Dr. Said Mirza Pahlevi, M.Eng.
II.I Selected Database Issues: 2 - Transaction ManagementSlide 1/20 1 II. Selected Database Issues Part 2: Transaction Management Lecture 4 Lecturer: Chris.
The Relational Model1 Transaction Processing Units of Work.
Section 06 (a)RDBMS (a) Supplement RDBMS Issues 2 HSQ - DATABASES & SQL And Franchise Colleges By MANSHA NAWAZ.
Transaction Management Transparencies. ©Pearson Education 2009 Chapter 14 - Objectives Function and importance of transactions. Properties of transactions.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L12_JDBC_MySQL 1 Transations.
Module 11: Managing Transactions and Locks
9 1 Chapter 9_B Concurrency Control Database Systems: Design, Implementation, and Management, Rob and Coronel.
NOEA/IT - FEN: Databases/Transactions1 Transactions ACID Concurrency Control.
10 1 Chapter 10_B Concurrency Control Database Systems: Design, Implementation, and Management, Rob and Coronel.
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.
Chapter 13 Managing Transactions and Concurrency Database Principles: Fundamentals of Design, Implementation, and Management Tenth Edition.
9 1 Chapter 9 Transaction Management and Concurrency Control Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
SYSTEMS IMPLEMENTATION TECHNIQUES TRANSACTION PROCESSING DATABASE RECOVERY DATABASE SECURITY CONCURRENCY CONTROL.
Transaction Management
Transaction Management and Concurrency Control
Transaction Management and Concurrency Control
Transaction Management Transparencies
Transaction Properties
Chapter 10 Transaction Management and Concurrency Control
Database Security Transactions
Introduction of Week 13 Return assignment 11-1 and 3-1-5
Transaction management
Transactions and Concurrency
Presentation transcript:

Transaction Management Reading: CB, Ch. 22

Dept of Computing Science, University of Aberdeen2 In this lecture you will learn the problems of concurrency in multi-user DBMSs why the concept of a transaction is useful how locks can be used to serialise transactions the concept of deadlocks and how they can be resolved how DBMSs roll back transactions after software or hardware failures

Dept of Computing Science, University of Aberdeen3 We are here!

Dept of Computing Science, University of Aberdeen4 Transaction Management Overview Objectives: –schedule queries from multiple simultaneous users efficiently –keep the database in a consistent state Transaction management involves: –performing logical units of work (defn of transactions) –controlling concurrency - stop user tasks interfering –resolving conflicts - e.g. simultaneous update attempts –recovering from errors - restore DB to consistent state

Dept of Computing Science, University of Aberdeen5 The ACID Requirements For a Transaction A transaction should have certain well-defined properties: Atomicity - each unit of work is indivisible; all-or- nothing (transactions that don't complete must be undone or rolled-back) Consistency - a transaction transforms the database from one consistent state into another (intermediates may be inconsistent) Isolation - each transaction effectively executes independently - one transaction should not see the inconsistent/incomplete state of another transaction Durability - once a transaction is complete, its effects cannot be undone or lost (it can only be undone with a compensating transaction)

Dept of Computing Science, University of Aberdeen6 How Are Transactions Defined? In SQL, by default, each SQL statement is treated as a transaction (even if it affects multiple rows or tables): UPDATE Staff SET Salary = 1.1 * Salary; This is one atomic unit of work on the database. Multiple statements may be grouped together into a single transaction. Example: A new member of staff takes over a property for rent: BEGIN TRANSACTION UPDATE PropertyForRent SET StaffNo = 'SN99' WHERE StaffNo = 'SG37'; DELETE FROM Staff WHERE StaffNo = 'SG37'; COMMIT TRANSACTION

Dept of Computing Science, University of Aberdeen7 Concurrent Transactions The Lost Update Problem Suppose an account holds $100. If T1 deposits $100 and T2 withdraws $10, the new balance should be $190. With concurrent transactions, we could get: T1: UPDATE Account SET Balance = Balance + 100; T2: UPDATE Account SET Balance = Balance - 10; TimeT1T2Balance t1read(bal)$100 t2bal += 100read(bal)$100 t3write(bal)bal -=10$200 t4write(bal)$90 How did $100 vanish? This is the lost update problem

Dept of Computing Science, University of Aberdeen8 Serialising Transactions Clearly, one solution would be to serialise all transactions: –make first transaction finish before next one starts However, this would not be efficient on multi- user systems: – Only need to serialise transactions that refer to common tables – Not all transactions need serialisation (e.g. print a mailing list) – Some transactions might benefit from running simultaneously (e.g. if they both read the same tables)

Dept of Computing Science, University of Aberdeen9 Controlling Concurrency With Locks Locks may be used to serialise only those parts of a transaction that need it... Locks may have different levels of granularity: – Table locks - easy to implement, not so efficient – Row locks - more complicated, better performance – Page locks - used by the Memory Manager (not here) Transactions can cooperate by using locks to indicate their intention: – Read (shared) - want to read an object – Write (exclusive) - want to read and write an object

Dept of Computing Science, University of Aberdeen10 The Rules for Using Locks Suppose two transactions want to access a given row... T1 holdsNo lockReadWrite T2 wantsRead Then T2Acquires Waits T1 holdsNo lockReadWrite T2 wantsWrite Then T2AcquiresWaits

Dept of Computing Science, University of Aberdeen11 Preventing Lost Updates With Locks TimeT1T2Balance t1write_lock(bal)$100 t2read(bal)write_lock(bal)$100 t3bal += 100wait$100 t4write(bal)wait$200 t5unlock(bal)wait$200 t6read(bal)$200 t7bal -= 10$200 t8write(bal)$190 t9unlock(bal)$190 Note - if T2 started first, T1 would WAIT

Dept of Computing Science, University of Aberdeen12 Concurrent Transactions Inconsistent Analysis Problem Often, a single transaction may modify multiple rows... What if two transactions execute at the same time ? –User1 (T1): UPDATE Salary... –User2 (T2): SELECT SUM(Salary)... Depending on the timing, User2 might see: –Sum of all old salaries –Sum of all new salaries –Sum of some old + some new salaries (problem!!) This is called the inconsistent analysis problem Clearly, multiple locks are required...

Dept of Computing Science, University of Aberdeen13 Two-Phase Locking What if a transaction requires more than one lock? The transaction should: –acquire the locks as it needs them... –but only release the locks at the end of the transaction This is two-phase locking - So called because there is: –a growing phase - more & more locks are acquired –a shrinking phase - the locks are finally released Two-phase locking solves inconsistent analysis problem. Can you work out why?? Hint: consider read & write lock waiting rules...

Dept of Computing Science, University of Aberdeen14 Deadlocks Two-phase locking ensures serialisability, but it cannot prevent deadlocks Example: Suppose a pair of transactions are in the growing phase, and both need write-locks on objects A and B: TimeT1T2 t1Acquire AAcquire B t2Wait for BWait for A T3Deadlock As deadlocks are relatively rare, most DBMSs allow them to occur rather than attempt to prevent them...

Dept of Computing Science, University of Aberdeen15 Detecting Deadlocks Deadlocks can be detected if the Lock Manager maintains a transaction dependency graph, which is sometimes called a wait-for graph (WFG)... The dependency graph contains: –a node for each transaction T –an arc Ta Tb for each dependency T1 T2 T3 T4 T1 T2 means T1 is waiting for a resource held by T2 A cycle in the graph indicates deadlock...

Dept of Computing Science, University of Aberdeen16 Deadlock Resolution If the DBMS detects deadlock, it picks a victim transaction: –the victim is killed - rolled-back and re- scheduled –the other transaction proceeds... Strategies for picking the victim include: –pick youngest, oldest, or random transaction –consider amount done (or to do) by each transaction –pick deadlocked node in WFG with the most dependencies

Dept of Computing Science, University of Aberdeen17 The Journal File and Rolling Back Transactions Usually, a log of each step of transaction is written to a special journal file. Each record of the journal file contains (in order of time): – a transaction ID & timestamp – a before-image (if the operation is an update or delete) – an after-image (if the operation is an update or insert) Periodically, the DBMS flushes all memory to disc & writes a checkpoint record. The Journal file can then be used for: – rolling back transactions – system error recovery (roll back to last checkpoint)

Dept of Computing Science, University of Aberdeen18 Specifying Locking Modes in SQL ANSI SQL (and some storage engines in MySQL Server) supports several locking modes. These are specified according to the degree of concurrency/isolation required. Syntax: –SET TRANSACTION ISOLATION LEVEL level where level is one of: – SERIALIZABLE - essentially full table locking – REPEATABLE READ - like table locking (but another T may INSERT) – READ COMMITTED - like row locking (the default) – READ UNCOMMITTED - no locking (might see phantom rows)

Dept of Computing Science, University of Aberdeen19 Summary Transaction Management is largely concerned with implementing the ACID requirements: – Defining logical units of work - atomic transactions – Using rollback and transaction scheduling to maintain consistency – Using locks to stop transactions interfering - isolation – Using journaling to recover from system errors - durability In SQL, its up to the programmer to consider & specify: – how much concurrency/isolation is required...