Lecture 8: Transaction Management and Concurrency Control PDF
Document Details
Uploaded by Deleted User
Tags
Summary
This document covers Lecture 8 on Transaction Management and Concurrency Control. The lecture introduces fundamental concepts of database transactions, including their properties, types and management.
Full Transcript
Lecture 8 Transaction Management and Concurrency Control 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...
Lecture 8 Transaction Management and Concurrency Control 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 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel What is a Transaction? (continued) A logical unit of work that must be either entirely completed or aborted. Successful transaction changes the database from one consistent state to another. – One in which all data integrity constraints are satisfied. Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Evaluating Transaction Results Not all transactions update the database SQL code represents a transaction because database was accessed. Improper or incomplete transactions can have a devastating effect on database integrity: – Some DBMSs provide means by which user can define enforceable constraints based on business rules. – Other integrity rules are enforced automatically by the DBMS when table structures are properly defined, thereby letting the DBMS validate some transactions Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Transaction Properties Atomicity – Requires that all operations (SQL requests) of a transaction be completed. Durability – Indicates permanence of database’s consistent state. Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Transaction Properties (continued) Serializability – Ensures that the concurrent execution of several transactions yields consistent results. Isolation – Data used during execution of a transaction cannot be used by second transaction until first one is completed. Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Transaction Management with SQL ANSI has defined standards that govern SQL database transactions. Transaction support is provided by two SQL statements: COMMIT and ROLLBACK. ANSI standards require that, – when a transaction sequence is initiated by a user or an application program, – it must continue through all succeeding SQL statements. Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel The Transaction Log Stores – A record for the beginning of transaction – For each transaction component (SQL statement) Type of operation being performed (update, delete, insert) Names of objects affected by the transaction (the name of the table) “Before” and “after” values for updated fields Pointers to previous and next transaction log entries for the same transaction – The ending (COMMIT) of the transaction Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel A Transaction Log Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Concurrency Control Coordination of simultaneous transaction execution in a multiprocessing database system. Objective: to ensure transaction serializability in a multiuser database environment Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Concurrency Control – Important simultaneous execution of transactions over a shared database can create several data integrity and consistency problems. lost updates uncommitted data inconsistent retrievals Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Normal Execution of Two Transactions Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Lost Updates Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Correct Execution of Two Transactions Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel An Uncommitted Data Problem Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel The Scheduler Special DBMS program: establishes order of operations within which concurrent transactions are executed. Interleaves the execution of database operations to ensure serializability and isolation of transactions. Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel The Scheduler (continued) Bases its actions on concurrency control algorithms. Facilitates data isolation to ensure that two transactions do not update the same data element at the same time Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel 1-Concurrency Control with Locking Methods Lock – Guarantees exclusive use of a data item to a current transaction – Required to prevent another transaction from reading inconsistent data Lock manager – Responsible for assigning and policing the locks used by the transactions Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Lock Granularity Indicates the level of lock use. Locking can take place at the following levels: – Database – Table – Page – Row – Field (attribute) Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Lock Granularity (continued) Database-level lock – Entire database is locked Table-level lock – Entire table is locked Page-level lock – Entire diskpage is locked Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Lock Granularity (continued) Row-level lock – Allows concurrent transactions to access different rows of the same table, even if the rows are located on the same page Field-level lock – Allows concurrent transactions to access the same row, as long as they require the use of different fields (attributes) within that row Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel A Database-Level Locking Sequence Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel An Example of a Table-Level Lock Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Example of a Page-Level Lock Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel An Example of a Row-Level Lock Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Read/Write Conflict Scenarios: Conflicting Database Operations Matrix Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Lock Types Binary lock – Has only two states: locked (1) or unlocked (0) Exclusive lock – Access is specifically reserved for the transaction that locked the object. – Must be used when the potential for conflict exists. Shared lock – Concurrent transactions are granted Read access on the basis of a common lock. Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel An Example of a Binary Lock Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Two-Phase Locking to Ensure Serializability Defines how transactions acquire and relinquish locks Guarantees serializability, but it does not prevent deadlocks – Growing phase, in which a transaction acquires all the required locks without unlocking any data – Shrinking phase, in which a transaction releases all locks and cannot obtain any new lock Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Two-Phase Locking Protocol Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Two-Phase Locking to Ensure Serializability (continued) Governed by the following rules: – 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 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Transaction T: Transaction U: bal=B.getBalance() bal=B.getBalance() B.setBalance(bal*1.1) B.setBalance(bal*1.2) A.withdraw(bal/10) C.withdraw(bal/10) Operations Locks Operations Locks OpenTransaction bal=B.getBalance() lock B B.setBalance(bal*1.1) openTransaction A.withdraw(bal/10) lock A bal=B.getBalance() waits for T’s lock on B closeTransaction unlock A,B ……….. B.setBalance(bal*1.2) lock B C.withdraw(bal/10) lock C closeTransaction unlock B,C Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Why strict Two-phase locking? To prevent dirty reads and premature writes during the progress of a transaction until the transaction aborted or committed. Example of operations that locking protocol is applied: Many readers/single writer scheme: When we control the access to the object so that there can be several concurrent transactions reading or a single transaction writing an object, but not both. So 2 types of locks exist: - read locks - write locks Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel A pair of read operations on different transactions don’t conflict, so more than one transaction can share a read lock on an object. The read locks are called shared locks. The operation conflict rules: If a transaction T has performed a read operation on a particular object, then a concurrent transaction U must not write on that object until T commits or abort. If a transaction T has already performed a write operation on a particular object, then a concurrent transaction U must not read or write on that object until T commits or aborts. Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel for one object lock requested read write lock already set none OK OK read OK wait write wait wait locking protocol prevents the occurrence of lost update and inconsistent retrievals. Promotion of locks: -Is a conversion of a lock to a stronger lock. -When a transaction read an object it performs a read lock on it and then when it wants to write on the same object it promote it’s read lock to write lock. Shared locks cannot be promoted because it will lead to conflict between transactions. Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Use of locks in strict two-phase locking: 1. When an operation accesses an object within a transaction: a) If the object is not already locked, it is locked and the operation proceeds. b) If the object has a conflicting lock set by another transaction, the transaction must wait until it is unlocked. c) If the object has a non-conflicting lock set by another transaction, the lock is shared and the operation proceeds. d) If the object has already been locked in the same transaction, the lock will be promoted if necessary and the operation proceeds (where promotion is prevented by a conflicting lock) Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Use of locks in strict two-phase locking: (cont.) 2. When transaction is committed or aborted, the server unlocks all objects it locked for the transaction. - The transaction coordinator responsible for releasing the locks when a transaction commits or aborts. Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Deadlocks Condition that occurs when two transactions wait for each other to unlock data Possible only if one of the transactions wants to obtain an exclusive lock on a data item – No deadlock condition can exist among shared locks. Control through – Prevention – Detection – Avoidance Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel How a Deadlock Condition Is Created Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Deadlocks: The use of locks lead to deadlock. Transaction T Transaction U Operations Locks Operations Locks 1. A.deposite(100) write lock A 2. B.deposite(200) write lock B 3. B.withdraw(100) waits for U’s waits for U’s 4. A.withdraw(200) waits for T’s lock on A ……. ……….. …….. ……….. Deposit and withdraw methods are atomic. Each of them acquires lock on one account and then gets blocked when it tries to access the account that other one has locked. Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel 2- Concurrency Control with Time Stamping Methods Assigns a global unique time stamp to each transaction Produces an explicit order in which transactions are submitted to the DBMS Uniqueness – Ensures that no equal time stamp values can exist Monotonicity – Ensures that time stamp values always increase Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Wait/Die and Wound/Wait Schemes Wait/Die :Older transaction waits and the younger is rolled back and rescheduled. – Younger owns/ Older requires: Older waits for younger. – Older owns/Younger requires: Younger Dies (Resch.) Wound/Wait : Older transaction rolls back the younger transaction and reschedules it. – Younger owns/ Older requires: Older wounds younger (Resch.). – Older owns/Younger requires: Younger waits older. Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Wait/Die and Wound/Wait Concurrency Control Schemes Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Database Recovery Management Database recovery – Restores database from a given state, usually inconsistent, to a previously consistent state. – Based on the atomic transaction property. All portions of the transaction must be treated as a single logical unit of work, in which all operations must be applied and completed to produce a consistent database. – If transaction operation cannot be completed, transaction must be aborted, and any changes to the database must be rolled back (undone). Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Transaction Recovery Makes use of deferred-write and write-through. Deferred write – Transaction operations do not immediately update the physical database. – Only the transaction log is updated. – Database is physically updated only after the transaction reaches its commit point using the transaction log information. Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Transaction Recovery (continued) Write-through – Database is immediately updated by transaction operations during the transaction’s execution, even before the transaction reaches its commit point. – Database is recovered using the transaction log. Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel