Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data and Database Administration Chapter 12. Outline What is Concurrency Control? Background Serializability  Locking mechanisms.

Similar presentations


Presentation on theme: "Data and Database Administration Chapter 12. Outline What is Concurrency Control? Background Serializability  Locking mechanisms."— Presentation transcript:

1 Data and Database Administration Chapter 12

2 Outline What is Concurrency Control? Background Serializability  Locking mechanisms

3 What is Concurrency Control? The process of managing simultaneous operations against a database so that data integrity is maintained and the operations do not interfere with each other in a multi-user environment.

4 Background Most DBMS run in a multi-user environment, where several users are able to share data contained in a database. No data integrity problems occur when data is being read. But problems can arise when several users are updating data. When more than one transaction is being processed against a DB at the same time, the transactions are considered to be concurrent. The actions that must be made to ensure data integrity are called concurrency control actions.

5 Background (Contd.) Remember that CPU can process only one instruction at a time. As new transactions are submitted while other processing is occurring against the database, the transactions are usually interleaved. The CPU switches among the transactions so that some portion of each transaction is performed as the CPU addresses each transaction in turn.

6 Problems without Concurrency Control Lost Updates problem can occur when multiple users attempt to update database. Example Two users with joint account trying to withdraw cash at the same time using ATM at different locations. User AUser B 1.Read account balance($1000) 2.Withdraw $200 2.Withdraw $300 3.Write account balance($800) 3.Write account balance($700) ERROR time

7 Problems without Concurrency Control Inconsistent read problems can occur when one user reads data that has been partially updated by another user. This read will be incorrect, also called dirty read or unrepeatable read.

8 Serializabilty Concurrent transactions need to be processed in isolation so that they do not interfere with each other. If one transaction were entirely processes at a time before another one, no interference would occur. Procedures which emulate this are called serializable. Serializable schedules process transactions that will give results as if they had been processed one after the other. Schedules are designed such that transactions that will not interfere with each other can be run in parallel, e.g., transactions that access data from different tables in a database will not conflict with each other. Serializability is achieved by different ways, but locking mechanisms are the most common.

9 Locking Locking implies that any data retrieved by a user for updating must be locked, or denied to other users, until the update is completed or aborted. Locking mechanism enforces a sequential updating process that prevents erroneous updates. (See next slide to understand this) It is the pessimistic approach of concurrency control

10 Concurrency Control through Locking User AUser B 1.Read account balance 2.Lock account balance 1.Read account balance (denied) 2.Read account balance($1000) 2.Withdraw $200 3.Write account balance($800) 4. Unlock account balance 2.Lock account balance 3.Read account balance($800) 4.Withdraw $300 5.Write account balance($500) 6. Unlock account balance time

11 Locking Levels There are different locking levels (granularity)  Database Entire DB is locked & becomes unavailable. This level can be used during backups  Table Entire table containing a requested record is locked. This level can be used for bulk updates that will update entire table (e.g., salary raise)  Block or page The physical storage block (or page) containing the requested record is locked. This level is commonly implemented.  Record level Only the requested record (row) is locked. All other records within table are available.  Field level Only the particular field (column) in the requested record is locked.

12 Types of locks Shared (S locks or read locks)  Allows other transactions to read but not update a record or resource.  Placing a shared lock prevents another user from placing an exclusive lock, but not a shared lock, on that record. Exclusive (X locks or write locks)  Prevent other transaction from reading and therefore updating a record until unlocked.  Should be placed when updating records.  Placing an exclusive lock on a record prevents another user from placing any type of lock on that record

13 Deadlocks Locking may prevent erroneous updates but can lead to deadlocks problem. Deadlock: When two or more transactions lock a common resource, each one of them waits for the other one to unlock that resource. Deadlock prevention: users must lock all required records at the beginning of the transaction. Two-phase locking protocol is used (read yourself from book). Deadlock resolution: the DBMS detects and breaks the deadlock by backing out of the the deadlocked transactions.  Any changes made by that transaction up to the time of deadlock are removed,  The transaction is restarted when the resources become available.

14 Versioning Optimistic approach of concurrency control Already covered in previous lectures.


Download ppt "Data and Database Administration Chapter 12. Outline What is Concurrency Control? Background Serializability  Locking mechanisms."

Similar presentations


Ads by Google