CS Reading Packet: "Transaction management, part 2"

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

Chapter 7 (Cont.) Transaction Management and Concurrency Control

Transaction Management

Lecture 22 Concurrency Control Part 2

Graph-based protocols are an alternative to two-phase locking Impose a partial ordering on the set D = {d 1, d 2,..., d h } of all data items.

Multiversion Schemes to achieve Snapshot Isolation Timestamped Based Protocols

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

Chapter 13 : Concurrency Control

Advanced Databases. Lecture 9- Concurrency Control (continued) Masood Niazi Torshiz Islamic Azad University- Mashhad Branch

Chapter 15 : Concurrency Control


Comp 5311 Database Management Systems. 14. Timestamp-based Protocols

Chapter 22. Transaction Management

Concurrency Control. Concurrency Control Ensures interleaving of operations amongst concurrent transactions result in serializable schedules

Concurrency control CS 417. Distributed Systems CS 417

2 nd Semester 2009/2010

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

Concurrency Control in Distributed Systems. ECE 677 University of Arizona

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

TRANSACTION PROPERTIES

CS Final Exam Review Suggestions

Concurrency Control. Data Base Management Systems. Inherently Concurrent Systems: The requirements

Chapter 16 : Concurrency Control

A lock is a mechanism to control concurrent access to a data item Data items can be locked in two modes:

For more Articles Go To: Whatisdbms.com CONCURRENCY CONTROL PROTOCOL

Chapter 12 : Concurrency Control

Optimistic Concurrency Control. April 13, 2017

Concurrency Control 9-1

CSIT5300: Advanced Database Systems

Distributed Systems. 12. Concurrency Control. Paul Krzyzanowski. Rutgers University. Fall 2017

Concurrency Control Overview. COSC 404 Database System Implementation. Concurrency Control. Lock-Based Protocols. Lock-Based Protocols (2)

! A lock is a mechanism to control concurrent access to a data item! Data items can be locked in two modes :

Transactions and Concurrency Control. Dr. Philip Cannata

Lecture 13 Concurrency Control

Unit 10.5 Transaction Processing: Concurrency Zvi M. Kedem 1

CS Reading Packet: "Entity-relationship modeling, part 1"

Database Management Systems

Multi-User-Synchronization

Optimistic Concurrency Control. April 18, 2018

References. Concurrency Control. Administração e Optimização de Bases de Dados 2012/2013. Helena Galhardas e

UNIT 4 TRANSACTIONS. Objective

Last Lecture. More Concurrency. Concurrency So Far. In This Lecture. Serialisability. Schedules. Database Systems Lecture 15

BCA204T: DATA BASE MANAGEMENT SYSTEMS

Checkpoints. Logs keep growing. After every failure, we d have to go back and replay the log. This can be time consuming. Checkpoint frequently

DATABASE DESIGN I - 1DL300

Foundation of Database Transaction Processing. Copyright 2012 Pearson Education, Inc.

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

DATABASE TRANSACTIONS. CS121: Relational Databases Fall 2017 Lecture 25

Last Class Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications

CSE 530A ACID. Washington University Fall 2013

UNIT IV TRANSACTION MANAGEMENT

Chapter 10 : Concurrency Control

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

Concurrency Control Algorithms

Chapter 10: Concurrency Control! Chapter 10 : Concurrency Control! Intuition of Lock-based Protocols! Lock-Based Protocols!

Chapter 10 : Concurrency Control!

Distributed Databases Systems

DB2 Lecture 10 Concurrency Control

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

Transactions. Kathleen Durant PhD Northeastern University CS3200 Lesson 9

Atomic Transac1ons. Atomic Transactions. Q1: What if network fails before deposit? Q2: What if sequence is interrupted by another sequence?

Chapter 9: Concurrency Control

Distributed Database Management System UNIT-2. Concurrency Control. Transaction ACID rules. MCA 325, Distributed DBMS And Object Oriented Databases

Transaction Management. Pearson Education Limited 1995, 2005

Transaction Management

Lock-based concurrency control. Q: What if access patterns rarely, if ever, conflict? Serializability. Concurrency Control II (OCC, MVCC)

ISSN: Monica Gahlyan et al, International Journal of Computer Science & Communication Networks,Vol 3(3),

Last Class Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications

CMP-3440 Database Systems

Concurrency Control. R &G - Chapter 19

CS Reading Packet: "Database Processing and Development"

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI

Transaction Processing: Basics - Transactions

CS352 Lecture - Concurrency

Overview. Introduction to Transaction Management ACID. Transactions

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

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

Part VII Data Protection

UNIT 2 Set Operators

Transaction Management: Introduction (Chap. 16)

T ransaction Management 4/23/2018 1

Multiversion schemes keep old versions of data item to increase concurrency. Multiversion Timestamp Ordering Multiversion Two-Phase Locking Each

Concurrency Control. Chapter 17. Comp 521 Files and Databases Fall

Today s Class. Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. Formal Properties of Schedules. Conflicting Operations

Chapter 5. Concurrency Control Techniques. Adapted from the slides of Fundamentals of Database Systems (Elmasri et al., 2006)

Database Management Systems 2010/11

Concurrency Control. Chapter 17. Comp 521 Files and Databases Spring

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

Lock-based Concurrency Control

Implementing Isolation

Module 6: Process Synchronization. Operating System Concepts with Java 8 th Edition

CMSC 424 Database design Lecture 22 Concurrency/recovery. Mihai Pop

Concurrency Control. Chapter 17. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

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

Transactions and Concurrency Control

Concurrency Control - Two-Phase Locking

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

Intro to Transactions

CS352 Lecture - Concurrency

Lecture 21 Concurrency Control Part 1

Transcription:

CS 325 - Reading Packet: "Transaction management, part 2" p. 1 Sources: CS 325 - Reading Packet: "Transaction management, part 2" * Ricardo, "Databases Illuminated", Chapter 10, Jones and Bartlett. * Kroenke, "Database Processing: Fundamentals, Design, and Implementation", 7th edition, Prentice Hall, 1999. * Rob and Coronel, "Database Systems: Design, Implementation, and Management", 3rd Edition, International Thomson Publishing, 1997. * Connolly and Begg, "Database Systems: A Practical Approach to Design Implementation and Management", 3rd Edition, Addison-Wesley. * Korth and Silberschatz, "Database System Concepts" * Sunderraman, "Oracle 9i Programming: A Primer", Addison-Wesley. * Ullman, "Principles of Database Systems", 2nd Edition, Computer Science Press. Transaction Management and Concurrency Control - Part 2 As we mentioned at the end of the previous reading packet, three classic categories of algorithms for concurrency control are: * locks * timestamping * optimistic methods Now, we will discuss some examples of these algorithms. Locks In locking (sometimes called resource locking), the idea is that we prevent some of the anomalies and inconsistencies mentioned, due to lost updates, dirty reads, etc., by preventing multiple transactions from having copies of the same data when that data may be about to be changed. You selectively restrict concurrency when information may be updated -- transactions must obtain a lock for data before they are permitted to use it (read it or write it), and the transaction then releases that lock when it is done, so another transaction can obtain a lock on that information if it needs to. (Note that, in this discussion, we are assuming implicit locks, locks placed automatically by the DBMS, which is the norm for multi-user DBMS's that automatically initiate and enforce locking procedures, managed by a lock manager. Explicit locks would be locks issued as a result of commands made by the application program or query user to the DBMS, and we will not be discussing those here.) Also note that lock granularity is another factor that can vary within a locking approach. Lock granularity is how much (or how little) you can lock at once; that is, when you obtain lock, how much is locked? A single cell within a row? a row? a column? a page? a table? the entire database?! Note the

CS 325 - Reading Packet: "Transaction management, part 2" p. 2 tradeoff between these different lock granularities: * larger granularity is easier for the DBMS to administer, but creates more (potentiallyunnecessary) conflict, and could reduce potential "safe" concurrency; * consider locking a row versus locking a whole table -- it is less likely that two transactions will want to access the same row at the same time than that two transactions will want to access different rows of the same table at the same time. If the lock granularity is one row, then different transactions accessing different rows of that table -- that could safely proceed concurrently -- will be able to do so, but if the lock granularity is the whole table, they will not be permitted to, even though it would be "safe"; one will have to wait. * smaller granularity is more difficult to administer (more details for the DBMS to keep track of and check, and more overhead for the locks), but conflict is less frequent, and more potential "safe" concurrency is possible. There are a wide variety of locking algorithms and techniques; here we just mention a couple of the most classic approaches, binary locks and shared/exclusive locks. Binary locks Binary locks have only 2 states, 1 and 0 (locked and unlocked). If something is locked by one transaction, then no other transaction can use it, period, until that lock is released; if something is unlocked, then any transaction can lock it for its use. ("Something", here, being whatever lock granularity the DBMS is using.) Every database operation requires that the affected object be locked, and as a rule a transaction must unlock the object after its termination. So, every transaction requires a lock and unlock operation for every item to be accessed. (But, since we are assuming implicit locking, remember, this will be automatically managed and scheduled by the DBMS.) Notice that this is relatively simple for the DBMS -- when a binary lock is requested, the DBMS determines if that something is already locked. If so, the requesting transaction has to wait; if not, the requesting transaction obtains the lock. That is: Transaction T wants a binary lock on item I. Does it get it? * if item I is currently locked, transaction T has to wait; * if item I is currently unlocked, transaction T obtains the binary lock. It can require as little as one bit to store the lock's state (since its only two states are 1 or 0), so the overhead is fairly low as well (although lock granularity will affect how much overhead is required). However, binary locking is also relatively restrictive, limiting potential "safe" concurrency: for example, concurrent reads are safe, but they will not be allowed under binary locking. Shared/Exclusive locks (Read/Write locks) Shared/exclusive locks (also called read/write locks) have 3 states: shared (or read) locked, exclusive (or write) locked, and unlocked. An exclusive lock (write lock) locks the item from any other concurrent access; the transaction with an exclusive lock can both read and write the item, and no other transaction may do so while that item is exclusive-locked. (A transaction must obtain an exclusive lock on something before updating it!)

CS 325 - Reading Packet: "Transaction management, part 2" p. 3 A shared lock (read lock) locks the item from being changed but not from being read; multiple transactions are permitted to obtain a shared lock on the same item at the same time. Note the increase in potential concurrency -- you permit concurrent reads when it is "safe" to do so (when the object does not have an exclusive lock). Notice that the algorithm, here, is a little more involved than for binary locking; consider: * transaction T wants a shared/read lock on item I; does it get it? * if item I is currently unlocked: YES, transaction T gets the shared lock; * if item I is currently shared-locked: YES, transaction T gets the shared lock; * if item I is currently exclusive-locked: NO, transaction T does not get the shared lock, and has to wait until the exclusive lock is released. * transaction T wants an exclusive/write lock on item I; does it get it? * if item I is currently unlocked: YES, transaction T gets the exclusive lock; * if item I is currently shared-locked: NO, transaction T does not get the exclusive lock, and has to wait until (all the) shared lock(s) are released; * if item I is currently exclusive-locked: NO, transaction T does not get the exclusive lock, and has to wait until the exclusive lock is released. Note that there is a little more overhead here -- you need at least 2 bits to represent the 3 possible lock states of unlocked, shared-locked, and exclusive-locked, and that's before you handle keeping track of how many transactions are currently have a shared lock on some item -- and the algorithm is a little more involved, but there is also more potential "safe" concurrency, especially if reads are more frequent than updates. Two-phased locking Note that locking helps to achieve isolation, but it does not, by itself, ensure serializability; that is, using locks alone does not necessarily result in a serializable transaction schedule. Additional protocols must be added to locking to ensure serializability. One classic protocol for this is two-phased locking; serializability can be guaranteed if a two-phased locking protocol is used: * with this strategy, transactions may obtain locks as necessary, but once the first lock (of any kind) is released, no other lock can be obtained. * and, it is called two-phased because, when you enforce the above, it results in a transaction having a growing phase, in which locks are obtained, and a shrinking phase, in which locks are released. * Note that a more-restrictive (and easier-to-implement) variation of two-phased locking, used by DB2 (and some other DBMS s, also?), simply does not release any locks until a COMMIT or ROLLBACK command is issued; * so, no lock is released until the transaction has essentially been completely done, or completely not done;

CS 325 - Reading Packet: "Transaction management, part 2" p. 4 * note that locks can thus be obtained throughout the transaction...the shrinking phase simply does not begin until the transaction is complete or aborted. Deadlocks While locking helps solve inconsistency problems due to concurrent transactions, it introduces another class of problems...deadlock conditions! Consider the following scenario: alloc A req pencil record paper record alloc req B * A wants to get some pencils, and if they can get them, then they want to get paper * B wants to get some paper, and if they can get them, then they want to get pencils So... 1. A requests and obtains a lock on paper; 2. B requests and obtains a lock on pencil; 3. A requests a lock on pencil, but cannot get it, and so has to wait; 4. B requests a lock on paper, but cannot get it, and so has to wait. See the problem? A and B are each waiting for something that the other has locked; they are locked in a so-called deadly embrace, they are in a state of deadlock. Strategies for Deadlock Management There are a number of strategies for deadlock management; those strategies include (but are not limited to): * timeouts * detection * prevention Timeouts If a transaction requests a lock and has to wait, it will only wait for a system-defined period of time. [Connolly and Begg, p. 570] If a lock request times out, then the DBMS assumes that the transaction MAY be deadlocked (although it might not be...!) and aborts and automatically restarts it. This strategy is very simple, practical, and Connolly and Begg notes that it "is used by several commercial DBMS's".

CS 325 - Reading Packet: "Transaction management, part 2" p. 5 Detection Detection strategies allow deadlock to occur, detect it, and then break it. That is, the DBMS periodically tests the database for deadlocks (using, for example, a wait-for graph --- a dependencies graph. For example, it can build a dependencies graph based on what transactions are waiting for what items, and then look for cycles within that graph: any such cycle is a deadlock.). Once detected, the DBMS breaks the deadlock by selecting one of the deadlocked transactions, aborting it, and restarting it. Prevention Prevention strategies prevent deadlock from occurring at all. For example, a transaction requesting a new lock will be aborted if there is a possibility that a deadlock can occur as a result of that lock request -- (and remember that, based on two-phased locking, aborting a transaction causes all of its locks obtained up to that point to be released). The aborted transaction is the rescheduled for execution. Prevention works because it avoids the conditions that lead to deadlock; however, Connolly and Begg claim that these strategies are more difficult and generally avoided. Timestamping Algorithms Timestamping algorithms are an alternative to locking approaches for scheduling concurrent transactions; they are another, different class of algorithms for this. In timestamping algorithms, a time stamp is assignment to each transaction when it is started. This time stamp is not based on a 24-hour clock, however -- it has some very particular requirements: * it must be global * it must be unique for each transaction * it must be monotonic (it must have the property of monotonicity -- the time stamp values must always increase) All database operations within the same transaction will be considered to have the transaction's time stamp. The DBMS then ensures that conflicting operations are performed in time stamp order, thereby ensuring serializability of the transactions. (Do you see why this assures serializability? Because the concurrent transactions' effects thus must be the same as a serial order of those transactions, the serial order of their happening to be executed in time stamp order!) What if a transaction's conflicting operation would have to violate time stamp order to be done? Then that transaction will be aborted, rescheduled, and assigned a new (and larger) time stamp when it is started again. Going into a bit more detail about this approach: with each data item Q, you associate two timestamp values: * W-ts(Q) - the largest time stamp of any transaction that executed write(q) successfully * R-ts(Q) - the largest time stamp of any transaction that executed read(q) successfully Assume that Ti is a transaction, and that TS(Ti) is the time stamp of transaction Ti. Then the timestamp-ordering protocol ensures that any conflicting reads and writes are executed in timestamp order (thus ensuring serializability):

CS 325 - Reading Packet: "Transaction management, part 2" p. 6 * If Ti issues read(q): * if TS(Ti) < W-ts(Q), then Ti needs to read a value of Q that was "already" overwritten by a "later" transaction (according to timestamp ordering); * the read(q) will be rejected, and Ti will be aborted, rolled back, and restarted with a new (larger) time stamp; * else (if TS(Ti) >= W-ts(Q)), then it is "safe" to execute the read(q); * the read(q) will be executed, and * R-ts(Q) will be set to the maximum of R-ts(Q) and TS(Ti) * (make sure that this is clear! You never want to set R-ts(Q) to be smaller than it was before; it always needs to have the largest time-stamp of a "successful" read! If the latest successful read(q) has a smaller time stamp than the current R-ts(Q), then R- ts(qp should be UNCHANGED.) * (it is important to note the time stamp of the "latest" read, so that another write does not get done "before" this read, in terms of time stamp ordering of conflicting operations) * If Ti issues write(q): * if TS(Ti) < R-ts(Q), then a "later" transaction has "already" read and used Q -- overwriting it now would be bad (and would violate time stamp ordering), because there is no way now for that "later" transaction to see the new value. * the write(q) will be rejected, and Ti will be aborted, rolled back, and restarted with a new (larger) time stamp; * else if TS(Ti) < W-ts(Q), then Ti is trying to write an "obsolete" value of Q (already overwritten, according to time stamp ordering) * the write(q) will be rejected, and Ti will be aborted, rolled back, and restarted with a new (larger) time stamp; * else it IS safe to execute write(q), so: * the write(q) is executed, and * W-ts(Q) is updated to be TS(Ti) Note that there is more overhead for this approach, the overhead for these timestamps! Each item modified needs this R-ts and W-ts stored for it. But while livelock/starvation could occur, deadlock is not an issue here, and it is an interesting alternative to locking algorithms for concurrency control. Optimistic methods This category of algorithms for concurrency control is based on the assumption that the majority of database operations do not conflict. These algorithms do not require locking or time-stamping -- instead, a transaction is executed without restrictions until it is committed. That is, the transaction moves through three phases:the read phase, the validation phase,and the write phase: * read phase: the transaction reads the database, executes the needed computations, and makes the

CS 325 - Reading Packet: "Transaction management, part 2" p. 7 updates to a private copy of the database values; * all updates are recorded in a temporary update file, accessible only by that one transaction (and not any others running simultaneously) * validation phase: the transaction is validated to assure that the changes made will not affect the database's integrity or consistency; * if the validation test succeeds? The transaction goes to the write phase. * if it fails? The transaction is restarted, and its changes discarded (they were made in a temporary update file, remember, and not to the actual database) * write phase: the changes are permanently applied to the database. This approach is acceptable for mostly-read or mostly-query database systems that require very few update transactions. We could take this discussion of concurrency control options much further, but this is where we will stop. Hopefully it has given you an idea of some of the different means of providing concurrency control and some of the issues involved.