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

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
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
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 Administration Part 1 Chapter Six CSCI260 Database Applications.
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.
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 2 Resources Examples of computer resources –printers –tape drives –tables Processes need access to resources in reasonable order Suppose.
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.3. The ostrich algorithm 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance.
Operating Systems 软件学院 高海昌 Operating Systems Gao Haichang, Software School, Xidian University 22 Contents  1. Introduction** 
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
Chapter 10 Transaction Management and Concurrency Control
Concurrency Control WXES 2103 Database.
Chapter 10 Transaction Management and Concurrency Control
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
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
Presentation transcript:

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

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

What Is a Transaction? 4A consistent state is one which all data integrity constrains are satisfied. u A transaction that changes the contents of the database must alter the database from one consistent database state to another. u 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

Example Of A Transaction u As this transaction is taking place, the DBMS must ensure that no other transaction access X. Figure 9.1 Read  Modify  Write

Example Of A Transaction u A transaction is a logical unit of work that must be either entirely completed or aborted Y = 50 Y = 60 X = X - 10 Y = Y + 10

What Is a Transaction? 4Evaluating Transaction Results u Examining the current balance for an account: SELECT ACC_NUM, ACC_BALANCE FROM CHECKACC WHERE ACC_NUM = ‘ ’; l represents a transaction because we accessed the database. l The database remains in a consistent state after the transaction, because it did not alter the database.

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

What Is a Transaction? 4Evaluating Transaction Results u The DBMS does not guarantee that the semantic meaning of the transaction truly represents the real-world event. l Although the syntax of the following UPDATE command is correct, its use yields incorrect results. UPDATE PRODUCT SET PROD_QOH = PROD_QOH + 10 WHERE PROD_CODE = ‘X’;

What Is a Transaction? 4Transaction Properties u Atomicity l All transaction operations must be completed l Incomplete transactions aborted u Durability permanence of the database’s consistent state. u Serializability l concurrent transactions are treated as though they were executed in serial order (one after another). l important in multi-user and distributed databases. u 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.

What Is a Transaction? 4Transaction Management with SQL u Transaction support is provided COMMIT ROLLBACK u 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 ).

What Is a Transaction? 4Transaction Management with SQL u Example: UPDATE PRODUCT SET PROD_QOH = PROD_QOH WHERE PROD_CODE = ‘345TYX’; UPDATE ACCREC SET AR_BALANCE = AR_BALANCE WHERE AR_NUM = ‘ ’; 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 ;

What Is a Transaction? 4The Transaction Log u 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. u 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. u The transaction log is itself a database, and it is managed by the DBMS like any other database.

A Transaction Log Table 9.1 Before After

Concurrency Control 4Concurrency control coordinates simultaneous execution of transactions in a multiprocessing database. u The objective of concurrency control is to ensure the serializability of transactions in a multi- user database environment. u Simultaneous execution of transactions over a shared database can create several data integrity and consistency problems: l Lost Updates. l Uncommitted Data. l Inconsistent retrievals.

Concurrency Control 4Lost Updates u Two concurrent transactions update PROD_QOH: u See Table 9.2 for the serial execution under normal circumstances. u See Table 9.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 T2: Sell 30 units PROD_QOH = PROD_QOH - 30

Concurrency Control 4Uncommitted Data u 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 (Rolled back) T2: Sell 30 units PROD_QOH = PROD_QOH - 30

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

Retrieval During Update Table 9.6 T1T2

Transaction Results: Data Entry Correction Table 9.7

Inconsistent Retrievals Table 9.8

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

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

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

4Lock Granularity u Lock granularity indicates the level of lock use. l Database level (See Figure 9.2) l Table level (See Figure 9.3) l Page level (See Figure 9.4) l Row level (See Figure 9.5) l Field level Concurrency Control with Locking Methods

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

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

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 9.4

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

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

An Example Of A Binary Lock Table 9.10

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

(2)Shared Locks 4A shared lock exists when concurrent transactions are granted READ access on the basis of a common lock. 4A 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. Concurrency Control with Locking Methods

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

4Shared/Exclusive Locks Current Want to Unlock Shared Lock Exclusive Lock Read Shared Lock Wait Write Exclusive Lock Wait

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

4Two-Phase Locking u The two-phase locking protocol defines how transactions acquire and relinquish locks. It guarantees serializability, but it does not prevent deadlocks. u 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. u In a shrinking phase, a transaction releases all locks and cannot obtain any new locks. Concurrency Control with Locking Methods

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

Two-Phase Locking Protocol Figure 9.6

4Deadlocks (Deadly Embrace) u Occurs when two transactions wait for each other to unlock data u 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 u 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) Concurrency Control with Locking Methods T1 T2 X Y requests holds

How A Deadlock Condition Is Created Table 9.11

Four Conditions for Deadlock All four of these conditions must be present : 1. Mutual exclusion condition each resource assigned to 1 process or is available 2. Hold and wait condition process holding resources can request additional 3. No preemption condition previously granted resources cannot forcibly taken away 4. 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

4Three Techniques to Control Deadlocks: u Deadlock Prevention A transaction requesting a new lock is aborted if there is a possibility that a deadlock can occur. u 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. u Deadlock Avoidance The transaction must obtain all the locks it needs before it can be executed. Avoid deadlocks by allocating resources carefully. Concurrency Control with Locking Methods

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

4An example for the deadlock detection algorithm 1. R 1 ! ≦ A 2. R 2 ! ≦ A 3. R 3 ≦ A → P 3 runs and releases → A=( ) 4. R 2 ≦ A → P 2 runs and releases → A=( ) 5. R 1 ≦ A → P 1 runs and releases → A=( ) Detection with Multiple Resource of Each Type

Deadlock Avoidance Two process Resource Trajectories 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

Safe and Unsafe States Demonstration that the state in (a) is safe (a) (b) (c) (d) (e) 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

Safe and Unsafe States Demonstration that the sate in (b) is not safe (a) (b) (c) (d) 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.

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

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

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

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

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

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

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

Concurrency Control with Optimistic Methods 4Optimistic Methods u Each transaction moves through two or three phases: l Read Phase: The transaction reads the database, executes the needed computations, and makes the updates to a private copy of the database values. All updates of the transaction are recorded in a temporary update file. l Validation Phase: The transaction is validated to assure that the changes made will not affect the integrity and consistency of the database. l Write Phase: The changes are permanently applied to the database. u acceptable for mostly read or query database systems that require very few update transaction.

Database Recovery Management 4Recovery restores a database from a given state, usually inconsistent, to a previously consistent state. 4Recovery 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.

Database Recovery Management 4Levels of Backup u Full backup of the database It backs up or dumps the whole database. u Differential backup of the database Only the last modifications done to the database are copied. u 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.

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

Database Recovery Management 4Transaction Recovery Procedures: u 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. u 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.