Chapter 10 Transaction Management and Concurrency Control

Slides:



Advertisements
Similar presentations
Concurrency Control WXES 2103 Database. Content Concurrency Problems Concurrency Control Concurrency Control Approaches.
Advertisements

TRANSACTION PROCESSING SYSTEM ROHIT KHOKHER. TRANSACTION RECOVERY TRANSACTION RECOVERY TRANSACTION STATES SERIALIZABILITY CONFLICT SERIALIZABILITY VIEW.
CSC271 Database Systems Lecture # 32.
Lock-Based Concurrency Control
Lecture 11 Recoverability. 2 Serializability identifies schedules that maintain database consistency, assuming no transaction fails. Could also examine.
Database Systems, 8 th Edition Concurrency Control with Time Stamping Methods Assigns global unique time stamp to each transaction Produces explicit.
Chapter 3 Deadlocks TOPICS Resource Deadlocks The ostrich algorithm
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.3. The ostrich algorithm 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance.
Avishai Wool lecture Introduction to Systems Programming Lecture 5 Deadlocks.
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
Transaction Management and Concurrency Control
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Transaction Management and Concurrency Control.
What is a Transaction? Logical unit of work
Chapter 8 : Transaction Management. u Function and importance of transactions. u Properties of transactions. u Concurrency Control – Meaning of serializability.
Transaction Management
Chapter 9 Transaction Management and Concurrency Control
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 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.
1 Transactions BUAD/American University Transactions.
Recovery & Concurrency Control. What is a Transaction?  A transaction is a logical unit of work that must be either entirely completed or aborted. 
Database Systems: Design, Implementation, and Management Tenth Edition
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 10 Transaction Management.
Chapter 9 Transaction Management and Concurrency Control Database Systems: Design, Implementation and Management Peter Rob & Carlos Coronel.
ITEC 3220M Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220m.htm
Ch 10: Transaction Management and Concurrent Control.
Concurrency Control in Database Operating Systems.
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.3. The ostrich algorithm 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance.
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.3. The ostrich algorithm 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance.
Chapter 9 Transaction Management and Concurrency Control Database Systems: Design, Implementation and Management Peter Rob & Carlos Coronel.
11/7/2012ISC329 Isabelle Bichindaritz1 Transaction Management & Concurrency Control.
II.I Selected Database Issues: 2 - Transaction ManagementSlide 1/20 1 II. Selected Database Issues Part 2: Transaction Management Lecture 4 Lecturer: Chris.
Transaction Management Transparencies. ©Pearson Education 2009 Chapter 14 - Objectives Function and importance of transactions. Properties of transactions.
Transaction Processing Concepts
ITEC 3220A Using and Designing Database Systems Instructor: Gordon Turpin Course Website: Office: CSEB3020.
1 Advanced Database Concepts Transaction Management and Concurrency Control.
Transaction Management and Concurrent Control
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.
10 Transaction Management and Concurrency Control MIS 304 Winter 2005.
©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.
10 1 Chapter 10 - A Transaction Management Database Systems: Design, Implementation, and Management, Rob and Coronel.
18 September 2008CIS 340 # 1 Last Covered (almost)(almost) Variety of middleware mechanisms Gain? Enable n-tier architectures while not necessarily using.
Chapter 13 Managing Transactions and Concurrency Database Principles: Fundamentals of Design, Implementation, and Management Tenth Edition.
Transactions and Concurrency Control. 2 What is a Transaction?  Any action that reads from and/or writes to a database may consist of  Simple SELECT.
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 Properties
Lecture 19: Deadlock: Conditions, Detection and Avoidance
Chapter 10 Transaction Management and Concurrency Control
Concurrency Control WXES 2103 Database.
Chapter 9 Transaction Management and Concurrency Control
Introduction of Week 13 Return assignment 11-1 and 3-1-5
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
Transaction management
Lecture 19: Deadlock: Conditions, Detection and Avoidance
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
Introduction to Deadlocks
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
Transactions, Properties of Transactions
Presentation transcript:

Chapter 10 Transaction Management and Concurrency Control Database Systems: Design, Implementation and Management Peter Rob & Carlos Coronel 1

What is a Transaction? Any action that reads from and/or writes to a database may consist of Simple SELECT statement to generate a list of table contents A series of related UPDATE statements to change the values of attributes in various tables A series of INSERT statements to add rows to one or more tables A combination of SELECT, UPDATE, and INSERT statements

What Is a Transaction? A transaction is a logical unit of work that must be either entirely completed or aborted; no intermediate states are acceptable. Most real-world database transactions are formed by two or more database requests. A database request is a single SQL statement in an application program or transaction. 4

What Is a Transaction? A consistent state is one which all data integrity constrains are satisfied. A transaction that changes the contents of the database must alter the database from one consistent state to another. To ensure consistency of the database, every transaction must begin with the database in a known consistent state. consistent state consistent state transaction database requests 4

What is a Transaction?

What Is a Transaction? Evaluating Transaction Results Examining the current balance for an account: SELECT CUST_NUMBER, CUST_BALANCE FROM CUSTOMER WHERE CUST_NUMBER = 10016; represents a transaction because we accessed the database. The database remains in a consistent state after the transaction, because it did not alter the database. 6

What Is a Transaction? Evaluating Transaction Results An accountant wishes to register the credit sale of 100 units of product X to customer Y in the amount of $500.00: Reducing product X’s Quantity on hand by 100. Adding $500.00 to customer Y’s accounts receivable. UPDATE PRODUCT SET PROD_QOH = PROD_QOH - 100 WHERE PROD_CODE = ‘X’; UPDATE CUSTOMER SET CUST_BALANCE = CUST_BALANCE + 500 WHERE CUST_NUMBER = ‘Y’; If the above two requests are not completely executed, the transaction yields an inconsistent database. The DBMS must be able to recover the database to a previous consistent state. A real-world transaction 7

Evaluating Transaction Results Figure 9.2

What Is a Transaction? Transaction Properties Atomicity All transaction operations must be completed Incomplete transactions aborted Consistency permanence of the database’s consistent state. Isolation means that the data used during the execution of a transaction cannot be used by a second transaction until the first one is completed. 9

What Is a Transaction? Transaction Properties Durability once transaction changes are done (committed), they cannot be undone. Serializability concurrent transactions are treated as though they were executed in serial order (one after another). Ensures that the concurrent execution of several transactions yields consistent results important in multi-user and distributed databases. 9

What Is a Transaction? Transaction Management with SQL Transaction support is provided COMMIT ROLLBACK When a transaction sequence is initiated, it must continue through all succeeding SQL statements until one of the following four events occurs: A COMMIT statement is reached. The COMMIT statement automatically ends the SQL transaction. A ROLLBACK statement is reached. The end of a program is successfully reached ( = COMMIT). The program is abnormally terminated ( = ROLLBACK). 10

What Is a Transaction? Transaction Management with SQL Example: UPDATE PRODUCT SET PROD_QOH = PROD_QOH - 2 WHERE PROD_CODE = ‘1558-QW1’; UPDATE CUSTOMER SET CUST_BALANCE = CUST_BALANCE + 87.98 WHERE CUST_NUMBER = ‘10011’; COMMIT; If UPDATE is the application’s last action and the application terminates normally  COMMIT is not necessary A transaction begins implicitly when the first SQL statement is encountered. Some SQL (not follow ANSI) use: BEGIN TRANSACTION; 11

What Is a Transaction? The Transaction Log A transaction log keeps track of all transactions that update the database. The information stored in the log is used by the DBMS for a recovery requirement triggered by a ROLLBACK statement or a system failure. The transaction log stores before-and-after data about the database and any of the tables, rows, and attribute values that participated in the transaction. The transaction log is itself a database, and it is managed by the DBMS like any other database. 12

The Transaction Log Before After

Concurrency Control Concurrency control coordinates simultaneous execution of transactions in a multiprocessing database. The objective of concurrency control is to ensure the serializability of transactions in a multi-user database environment. 14

Concurrency Control Simultaneous execution of transactions over a shared database can create several data integrity and consistency problems: Lost Updates. Uncommitted Data. Inconsistent retrievals. 14

Concurrency Control Lost Updates Two concurrent transactions update PROD_QOH: See Table 10.2 for the serial execution under normal circumstances. See Table 10.3 for the lost update problems resulting from the execution of the second transaction before the first transaction is committed. TRANSACTION COMPUTATION T1: Purchase 100 units PROD_QOH = PROD_QOH + 100 T2: Sell 30 units PROD_QOH = PROD_QOH - 30 15

TABLE 10.2 TABLE 10.3 16

Concurrency Control Uncommitted Data Data are not committed when two transactions T1 and T2 are executed concurrently and the first transaction is rolled back after the second transaction has already accessed the uncommitted data – thus violating the isolation property of the transaction. TRANSACTION COMPUTATION T1: Purchase 100 units PROD_QOH = PROD_QOH + 100 (Rolled back) T2: Sell 30 units PROD_QOH = PROD_QOH - 30 17

TABLE 10.4 TABLE 10.5 18

Concurrency Control Inconsistent Retrievals Inconsistent retrievals occur when a transaction calculates some summary (aggregate) functions over a set of data while other transactions are updating the data. Example: T1 calculates the total quantity on hand of the products stored in the PRODUCT table. At the same time, T2 updates the quantity on hand (PROD_QOH) for two of the PRODUCT table’s products. 19

Retrieval During Update TABLE 10.6 T2 T1

Transaction Results: Data Entry Correction TABLE 10.7

Inconsistent Retrievals TABLE 10.8

Concurrency Control The Scheduler The scheduler establishes the order in which the operations within concurrent transactions are executed. The scheduler interleaves the execution of database operations to ensure serializability and isolation. To determine the appropriate order, the scheduler bases its actions on concurrency control algorithms, such as locking or time stamping methods. The scheduler also makes sure that the computer’s CPU is used efficiently. 23

Read/Write Conflict Scenarios: Conflicting Database Operations Matrix TABLE 10.9 T1 and T2 are executed concurrently over the same data.

Concurrency Control with Locking Methods Concurrency can be controlled using locks. A lock guarantees exclusive use of a data item to a current transaction. A transaction acquires a lock prior to data access; the lock is released (unlocked) when the transaction is completed. All lock of information is managed by a lock manager. 25

Concurrency Control with Locking Methods Lock Granularity Lock granularity indicates the level of lock use. Database level (See Figure 10.3) Table level (See Figure 10.4) Page level (See Figure 10.5) Row level (See Figure 10.6) Field level (attribute) 26

A Database-Level Locking Sequence T1 and T2 cannot access the same database concurrently, even if they use different tables. T1( Update Table A ) T2( Update Table B ) FIGURE 10.3 27

An Example Of A Table-Level Lock T1 and T2 cannot access the same table concurrently, even if they use different rows. T1( Update Row 5 ) T2( Update Row 30 ) FIGURE 10.4 28

An Example Of A Page-Level Lock T1 and T2 cannot access the same page concurrently, even if they use different rows. the most frequently used multiuser DBMS locking methods. FIGURE 10.5

An Example Of A Row-Level Lock Although it improves the availability of data, its management requires high overhead cost. row FIGURE 10.6 30

Concurrency Control with Locking Methods Lock types Binary Locks Shared/Exclusive Locks A binary lock has only two states: locked (1) or unlocked (0). If an object is locked by a transaction, no other transaction can use that object. If an object is unlocked, any transaction can lock the object for its use. A transaction must unlock the object after its termination. 31

Binary Lock

Concurrency Control with Locking Methods Shared/Exclusive Locks (1)Exclusive Locks An exclusive lock exists when access is specially reserved for the transaction that locked the object. The exclusive lock must be used when the potential for conflict exists. issued when a transaction wants to update unlocked data item. 33

Concurrency Control with Locking Methods (2)Shared Locks A shared lock exists when concurrent transactions are granted READ access on the basis of a common lock. A shared lock produces no conflict as long as the concurrent transactions are read only. issued when a transaction wants to read data and no exclusive lock is held on that data item. 33

Concurrency Control with Locking Methods Shared/Exclusive Locks Although the possibility of shared locks renders data access more efficient, a shared/exclusive lock schema increases the lock manager’s overhead. Three lock operations needed: READ_LOCK(check the type of lock) WRITE_LOCK(issue the lock) UNLOCK(release the lock) 33

Concurrency Control with Locking Methods Shared/Exclusive Locks Current Want to Unlock Shared Lock Exclusive Lock Read Wait Write 33

Concurrency Control with Locking Methods Although locks prevent serious data inconsistencies, Potential Problems with Locks The resulting transaction schedule may not be serializable. The schedule may create deadlocks. Solutions Two-phase locking for the serializability problem. Deadlock detection and prevention techniques for the deadlock problem. 35

Concurrency Control with Locking Methods Two-Phase Locking The two-phase locking protocol defines how transactions acquire and relinquish locks. It guarantees serializability, but it does not prevent deadlocks. In a growing phase, a transaction acquires all the required locks without unlocking any data. Once all locks have been acquired, the transaction is in its locked point. In a shrinking phase, a transaction releases all locks and cannot obtain any new locks. 36

Concurrency Control with Locking Methods Rules for Two-Phase Locking Protocol Two transactions cannot have conflicting locks. No unlock operation can precede a lock operation in the same transaction. No data are affected until all locks are obtained – that is, until the transaction is in its locked point. 37

Two-Phase Locking Protocol

Concurrency Control with Locking Methods Deadlocks (Deadly Embrace) Occurs when two transactions wait for each other to unlock data Deadlocks exist when two transactions T1 and T2 exist in the following mode: T1 = requests X and holds Y T2 = requests Y and holds X If T1 has not unlocked data item Y, T2 cannot begin; and, if T2 has not unlocked data item X, T1 cannot continue. (See Table 9.11) T1 T2 X Y requests holds 39

How A Deadlock Condition Is Created Table 9.11 40

Four Conditions for Deadlock All four of these conditions must be present : Mutual exclusion condition each resource assigned to 1 process or is available Hold and wait condition process holding resources can request additional No preemption condition previously granted resources cannot forcibly taken away Circular wait condition must be a circular chain of 2 or more processes each is waiting for resource held by next member of the chain

Concurrency Control with Locking Methods Three Techniques to Control Deadlocks: Deadlock Prevention A transaction requesting a new lock is aborted if there is a possibility that a deadlock can occur. ( Attacking the four conditions ) Deadlock Detection The DBMS periodically tests the database for deadlocks. If a deadlock is found, one of the transactions (“victim”) is aborted, and the other transaction continues. Deadlock Avoidance The transaction must obtain all the locks it needs before it can be executed. Avoid deadlocks by allocating resources carefully. 41

Detection with Multiple Resource of Each Type n processes m resource classes Data structures needed by deadlock detection algorithm

Detection with Multiple Resource of Each Type An example for the deadlock detection algorithm R1 !≦ A R2 !≦ A R3 ≦ A → P3 runs and releases → A=(2 2 2 0) R2 ≦ A → P2 runs and releases → A=(4 2 2 1) R1 ≦ A → P1 runs and releases → A=(4 2 3 1)

Two process Resource Trajectories Deadlock Avoidance Based on the concept of safe states At point t , B is requesting plotter Grant- enter an unsafe region and eventually deadlock Deny- B suspended until A has requested and released plotter t Two process Resource Trajectories

Demonstration that the state in (a) is safe Safe and Unsafe States Safe state is not deadlock and there is some scheduling order in which every processes can run to completion even if all of them suddenly request their maximum number of resources immediately. (a) is safe because the system, by careful scheduling, can avoid deadlock. The system can guarantee that all processes will finish (a) (b) (c) (d) (e) Demonstration that the state in (a) is safe

Safe and Unsafe States Unsafe state The system cannot guarantee that all processes will finish is not deadlock state. The system can run for a while. Some process can even complete. It is possible that A might releases a resource before asking for any more, allowing C to complete and avoiding deadlock altogether. (a) (b) (c) (d) Demonstration that the sate in (b) is not safe

Deadlock Prevention (1) Attacking the Mutual Exclusion Condition (2) Attacking the Hold and Wait Condition (3) Attacking the No Preemption Condition (4) Attacking the Circular Wait Condition

Deadlock Prevention (1) Attacking the Mutual Exclusion Condition If no resources were ever assigned exclusively to a single process, we would never have deadlocks. Some devices (such as printer) can be spooled only the printer daemon ( only one process) requests printer resource thus deadlock for printer eliminated Not all devices can be spooled

(2) Attacking the Hold and Wait Condition Require processes to request all resources before starting a process never has to wait for what it needs Problems may not know required resources at start of run Resources will not be used optimally. Variation: process must give up all resources it currently holds then request all at once

(3) Attacking the No Preemption Condition This is not a viable option Consider a process given the printer halfway through its job now forcibly take away printer !!??

(4) Attacking the Circular Wait Condition Normally ordered resources- provide a global numbering of all the resources Resource allocation graph If i > j – A is denied If i < j – B is denied (a) (b)

Concurrency Control with Time Stamping Methods The time stamping approach assigns a global unique time stamp to each transaction to schedule concurrent transactions. The time stamp value produces an explicit order in which transactions are submitted to the DBMS. Time stamps must have two properties: Uniqueness assures that no equal time stamp values can exist. Monotonicity assures that time stamp values always increase. The DBMS executes conflicting operations in time stamp order to ensure the serializability. 42

Wait/Die and Wound/Wait Schemes Wait/die (Owning) Older transaction waits and the younger is rolled back and rescheduled Wound/wait (Older) Older transaction rolls back the younger transaction and reschedules it Requesting Lock Owning Lock Wait/Die Wound/Wait T1(Older) T2(Younger) T1 Wait T1 preempts T2 T2 Dies T2 Waits

Wait/Die and Wound/Wait Concurrency Control Schemes

Concurrency Control with Optimistic Methods It is based on the assumption that the majority of the database operations do not conflict. A transaction is executed without restrictions until it is committed. 43

Database Recovery Management Recovery restores a database from a given state, usually inconsistent, to a previously consistent state. Recovery techniques are based on the atomic transaction property: All portions of the transaction must be applied and completed to produce a consistent database. If, for some reason, any transaction operation cannot be completed, the transaction must be aborted, and any changes to the database must be rolled back. 44

Database Recovery Management Levels of Backup Full backup of the database It backs up or dumps the whole database. Differential backup of the database Only the last modifications done to the database are copied. Backup of the transaction log only It backs up all the transaction log operations that are not reflected in a previous backup copy of the database. 45

Database Recovery Management Database Failures Software Operating system, DBMS, application programs, viruses Hardware Memory chip errors, disk crashes, bad disk sectors, disk full errors Programming Exemption Application programs (a withdrawal of zero balance account) end users ( [Ctrl]+[C] ) Transaction Deadlocks External Fire, earthquake, flood 46

Database Recovery Management Transaction Recovery Procedures: Deferred-write/Deferred-update Transaction operations do not immediately update the database. Instead, all changes are written to the transaction log. The database is updated only after the transaction reaches its commit point. Write-through/Immediate-update The database is immediately updated by transaction operations during the transaction’s execution, even before the transaction reaches its commit point. The transaction log is also updated. If a transaction fails, the database uses the log information to roll back the database. 47