CMP-3440 Database Systems Concurrency Control with Locking, Serializability, Deadlocks, Database Recovery Management Lecture 10 zain 1
Basic Recovery Facilities Backup Facilities: provides periodic backup copies of portions or the entire database. Cold Backups, Hot Backups, Full Backups, Incremental Backups. Journalizing Facilities: maintains an audit trail of transactions and database changes. Transaction Log, Database Change Log, Security Log. Checkpoint Facility: periodically suspends all processing and synchronizes its files and journals to establish a recovery point. Recovery Manager: is a module that allows DBMS to restore the database to a correct condition and restart processing transactions. 2
Recovery Types Backward Recovery Forward Recovery 3
Transaction 4
Concurrent Access A database without concurrency control will be compromised with concurrent access due to interference between users. If users a reading, no data integrity problems. When more than one transactions are being processed at a same time, and sharing same object. 5
Concurrent Access 6
Lost Updates When multiple users attempt to update a database without adequate concurrency control. Inconsistent read problem, when user reads data that have been partially updated by another user. Both problems arise then DBMS does not isolate transactions. 7
Serializability See only data that was committed at the start of the transaction plus changes made by transaction itself through INSERTs, UPDATEs, DELETEs. Concurrent transactions need to be processed in isolation. Processing transactions using a serializable schedule ensures transactions had been processed one after the other in a serial manner. Transactions requesting data from different tables at a same time will not conflict. Locking mechanism is known to control concurrency issues. 8
Serializability SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; 9
Locking Mechanism Any data that are retrieved by a user for updating must be locked, or denied to other users, until the update is complete or aborted. Implementing locking level is the extent of database resource that is included in with each lock. Database: entire database is locked, maybe during a backup. Table: entire table is locked, maybe during a bulk update. Row: entire row is locked, maybe during WHERE clause. Field: a particular field or column is locked. 10
Type of Locks Shared Locks: Are read locks, allow other transactions to read but not update. Placing a shared lock on a record prevents another user from placing an exclusive lock. Exclusive Locks: Are write locks, prevent another transaction from reading and updating a record, until it is unlocked. Placing an exclusive lock on a record prevents another user from placing any lock. 11
Deadlock Locking may lead to a problem called deadlock. When two or more transactions have locked a common resource. And each must wait for the other to unlock that resource. 12
Deadlock 13
Example Time Session 1 Session 2 Explanation t0 SQL> UPDATE employees SET salary = salary*1.1 WHERE employee_id = 100; 1 row updated. SQL> UPDATE employees SET salary = salary*1.1 WHERE employee_id = 200; 1 row updated. Session 1 starts transaction 1 and updates the salary for employee 100. Session 2 starts transaction 2 and updates the salary for employee 200. No problem exists because each transaction locks only the row that it attempts to update.
Example Time Session 1 Session 2 Explanation t1 SQL> UPDATE employees SET salary = salary*1.1 WHERE employee_id = 200; -- prompt does not return SQL> UPDATE employees salary = salary*1.1 WHERE employee_id = 100; -- prompt does not return Transaction 1 attempts to update the employee 200 row, which is currently locked by transaction 2. Transaction 2 attempts to update the employee 100 row, which is currently locked by transaction 1. A deadlock results because neither transaction can obtain the resource it needs to proceed or terminate.
Managing Deadlock Deadlock Prevention: User programs must lock all records they will require at the beginning of transaction, rather than one at a time. Unfortunately it is often difficult to predict in advance what records will be required to process a transaction. Thus not always practical. Deadlock Resolution: Allow deadlocks to occur but to detect and break deadlocks. Backs out one of the deadlocked transactions, rolls back. 16