Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recovery & Concurrency Control. What is a Transaction?  A transaction is a logical unit of work that must be either entirely completed or aborted. 

Similar presentations


Presentation on theme: "Recovery & Concurrency Control. What is a Transaction?  A transaction is a logical unit of work that must be either entirely completed or aborted. "— Presentation transcript:

1 Recovery & Concurrency Control

2 What is a Transaction?  A transaction is a logical unit of work that must be either entirely completed or aborted.  A database request is the equivalent of a single SQL statement in an application program or transaction.  A transaction that changes the contents of the database must alter the database from one consistent database state to another.  To ensure consistency of the database, every transaction must begin with the database in a known consistent state.

3 Example of Transaction X = 100 X = 50 X = X - 50 Amount in stock = X Initial State Final State Transaction A

4 Transaction ACID Properties  Atomic All operations of a transaction be completed; if not, the transaction is aborted. Transaction cannot be subdivided  Consistent Constraints don’t change from before transaction to after transaction  Isolated Data used during the execution of a transaction cannot be used by a second transaction until the first one is completed. Database changes not revealed to users until after transaction has completed  Durable Database changes are permanent The permanence of the database’s consistent state.

5 Transaction Management with SQL  Transaction support is provided by 2 SQL statements: 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. A ROLLBACK statement is reached. The end of a program is successfully reached (COMMIT). The program is abnormally terminated (ROLLBACK).

6 Transaction Management with SQL  Example: UPDATE PRODUCT SET PROD_QOH = PROD_QOH - 200 WHERE PROD_CODE = ‘QS123XY’; UPDATE ACCT_RECEIVABLE SET ACCT_BALANCE = ACCT_BALANCE + 10000 WHERE ACCT_NUM = ‘12345678’; COMMIT;

7 Concurrency Control  Problem–in a multi-user environment, simultaneous access to data can result in interference and data loss Concurrency Control  Solution–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

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

9 Lost Updates

10  Using Product Table: Product’s quantity on Hand (PROD_QOH)  Two concurrent transactions update PROD_QOH:  See Table 1 for the serial execution under normal circumstances.  See Table 2 for the lost update problems resulting from the execution of the second transaction before the first transaction is committed.

11 Lost Updates

12 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.

13 Uncommitted Data Correct Execution Of Two Transactions An Uncommitted Data Problem

14 Inconsistent Retrieval  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.

15 Inconsistent Retrieval

16

17

18 Concurrency Control Techniques  Serializability Finish one transaction before starting another  Locking Mechanisms The most common way of achieving serialization Data that is retrieved for the purpose of updating is locked for the updater No other user can perform update until unlocked

19 Updates with locking (concurrency control)

20 Locking Mechanisms  Locking level: Database–used during database updates Table–used for bulk updates Block or page–very commonly used Record–only requested row; fairly commonly used Field–requires significant overhead; impractical

21 Database Lock

22 Table Lock

23 Block or Page Lock

24 Record Lock

25 Locking Mechanisms  Types of locks: Shared lock (S locks/read locks) ○ Allows other transactions to read but not update a record or other resources ○ Read but no update permitted. Used when just reading to prevent another user from placing an exclusive lock on the record Exclusive lock (X locks/write locks) ○ Prevents another transactions from reading and therefore updating a record until it is locked. ○ No access permitted. Used when preparing to update

26 Deadlock

27

28 Managing Deadlock  Deadlock prevention: Lock all records required at the beginning of a transaction Two-phase locking protocol ○ Growing phase ○ Shrinking phase May be difficult to determine all needed resources in advance

29 Deadlock prevention  Two-phase locking protocol 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 Shrinking phase ○ A transaction releases all locks and cannot obtain any new lock

30 Two-phase locking protocol

31 Managing Deadlock  Deadlock Resolution: Allow deadlocks to occur nut build mechanisms into the DBMS for detecting and breaking the deadlocks. The DBMS maintains a matrix of resource usage. ○ Indicates what subjects (users) are using what objects (resources) ○ Scanning this matrix: the computer can detect deadlock if occur ○ DBMS resolve deadlock: One of the transactions is aborted: changes made by the transaction up to the time of deadlock are removed & transaction is restarted when the required resources become available.

32 Versioning  Optimistic approach to concurrency control  Instead of locking  Assumption is that simultaneous updates will be infrequent  Each transaction can attempt an update as it wishes  The system will reject an update when it senses a conflict  Use of rollback and commit for this

33 Figure 13-15 The use of versioning Better performance than locking

34 Data Dictionaries and Repositories  Data dictionary Documents data elements of a database Store metadata/information about the database Active: managed by DBMS Passive: managed by the user(s) Part of system catalog System catalog ○ System-created database that describes all database objects ○ Table-related data (table names), table creators/owners, column names, data types, etc

35 Data Dictionaries and Repositories  Information Repository Stores metadata that describe an organization’s data and data processing resources, manages the total information processing environment and combines information about an organization's business information and its application portfolio  Information Repository Dictionary System (IRDS) Software tool that is used to manage and control access to the information repository  3 components of repository system architecture: Information Model Repository Engine Repository Database

36 Figure 13-16 Three components of the repository system architecture A schema of the repository information Software that manages the repository objects Where repository objects are stored Source: adapted from Bernstein, 1996.

37 Data Availability  Downtime is expensive Lost business during the outage Cost of catching up when service is restored Legal costs Permanent loss of customer loyalty  How to ensure availability Hardware failures–provide standby components Loss of data–database mirroring Human Error – SOP, training, documentation Maintenance downtime–automated and non-disruptive maintenance utilities Network problems–careful traffic monitoring, firewalls, and routers

38 Information in this slides were taken from Modern Database Management System, Ninth edition by Jeffrey A.Hoffer, Mary B.Prescott & Heikki Topi. AND Database Systems, Design, Implementation & Management by Peter Rob & Carlos Coronel


Download ppt "Recovery & Concurrency Control. What is a Transaction?  A transaction is a logical unit of work that must be either entirely completed or aborted. "

Similar presentations


Ads by Google