12
Concurrency Control Amity International Business School 2012-2015 BY : Sunil Kr. Ahirwar BBA-IB Sec- “A” III Semester- 2012-2015

Concurrency Control

Embed Size (px)

DESCRIPTION

DBMS Topic Concurrency Control

Citation preview

Page 1: Concurrency Control

Concurrency Control

Amity International Business School 2012-2015

BY : Sunil Kr. AhirwarBBA-IB Sec- “A”III Semester- 2012-2015

Page 2: Concurrency Control

Concurrency Control

Process of managing simultaneous execution of transactions in a shared database, to ensure the serializability of transactions, is known as concurrency control. Concurrency Control Protocols are mechanisms to control concurrency of transactions to ensure isolation of transactions (or serializable schedules

AIBS BBA-IB-III Sec-A

Page 3: Concurrency Control

Need of Concurrency Control

1-Lost Updates: This problem occurs when two transactions that access the same database items have their operations interleaved in a way that makes the value of some database item incorrect. Successfully completed update is overridden by another user.

2-The dirty read problem: Transactions read a value written by a transaction that has been later aborted. This value disappears from the database upon abort, and should not have been read by any transaction ("dirty read"). The reading transactions end with incorrect results.

3-The incorrect summary problem: While one transaction takes a summary over the values of all the instances of a repeated data-item, a second transaction updates some instances of that data-item. The resulting summary does not reflect a correct result for any (usually needed for correctness) precedence order between the two transactions:

AIBS BBA-IB-III Sec-A

Page 4: Concurrency Control

In databases and transaction processing, two-phase locking (2PL) is a concurrency control method that guarantees serializability. It is also the name of the resulting set of database transaction schedules (histories). The protocol utilizes locks, applied by a transaction to data, which may block (interpreted as signals to stop) other transactions from accessing the same data during the transaction's life.By the 2PL protocol locks are applied and removed in two phases:Expanding phase: locks are acquired and no locks are released.Shrinking phase: locks are released and no locks are acquired.Two types of locks are utilized by the basic protocol: Shared and Exclusive locks. Refinements of the basic protocol may utilize more lock types. Using locks that block processes, 2PL may be subject to deadlocks that result from the mutual blocking of two or more transactions.

AIBS BBA-IB-III Sec-A

Two Phase Locking

Page 5: Concurrency Control

Concurrency Control TechniquesPessimistic concurrency control: Is an approach

where an entity is locked in the database for the entire time that it is in application memory.  A lock either limits or prevents other users from working with the entity in the database.

Optimistic concurrency control:This strategy is appropriate for single user systems, systems where the system of record is guaranteed to be accessed by only one user or system process at a time, or read-only tables.  These situations do occur.  It is important to recognize that this strategy is completely inappropriate for multi-user systems

AIBS BBA-IB-III Sec-A

Page 6: Concurrency Control

Advantages

By having concurrency control you avoid dirty writes (or inconsistent data). It is like your traffic signal - allows one set of users (drivers) to pass through at a time. In computer terms, if a user wants to update some data then the following steps are taken: (a) the data is fetched along with the timestamp (the date, time when it was last modified) (b) the data is modified by the user who then submits the data for update / storage in the system (c) the system verifies the original timestamp with the current timestamp. (i) If both are same, the new data (including the new timestamp) is updated/stored in the system. (ii) If they are different, then obviously the data had changed between the time it was fetched and now - meaning that someone else had updated in the meanwhile. The system typically informs the user that the data had been modified by a different user and asks him/her to submit the modifications once again.

AIBS BBA-IB-III Sec-A

Page 7: Concurrency Control

Concurrency control is a type of management style where employers or supervisors constantly monitor how employees are working while the work is still in progress. This kind of management makes employees feel like slaves and lowers their morale to work, which lowers production. It also creates a sense of mistrust between the employer and the employees.

AIBS BBA-IB-III Sec-A

Disadvantages

Page 8: Concurrency Control

Case Study

Concurrency control and lockingThe purpose of concurrency control is to prevent

two different users (or two different connections by the same user) from trying to update the same data at the same time. Concurrency control can also prevent one user from seeing out-of-date data while another user is updating the same data.

The following examples explain why concurrency control is needed. For both examples, suppose that your checking account contains $1,000. During the day you deposit $300 and spend $200 from that account. At the end of the day your account should have $1,100.

AIBS BBA-IB-III Sec-A

Page 9: Concurrency Control

Solution:

Example 1: No concurrency controlAt 11:00 AM, bank teller #1 looks up your account and

sees that you have $1,000. The teller subtracts the $200 check, but is not able to save the updated account balance ($800) immediately.

At 11:01 AM, another teller #2 looks up your account and still sees the $1,000 balance. Teller #2 then adds your $300 deposit and saves your new account balance as $1,300.

At 11:09 AM, bank teller #1 returns to the terminal, finishes entering and saving the updated value that is calculated to be $800. That $800 value writes over the $1300.

At the end of the day, your account has $800 when it should have had $1,100 ($1000 + 300 - 200).

AIBS BBA-IB-III Sec-A

Page 10: Concurrency Control

Example 2: Concurrency controlWhen teller #1 starts working on your

account, a lock is placed on the account.When teller #2 tries to read or update your

account while teller #1 is updating your account, teller #2 will not be given access and gets an error message.

After teller #1 has finished the update, teller #2 can proceed.

At the end of the day, your account has $1,100 ($1000 - 200 + 300).

AIBS BBA-IB-III Sec-A

Page 11: Concurrency Control

In Example 1, the account updates are done simultaneously rather than in sequence and one update write overwrites another update. In Example 2, to prevent two users from updating the data simultaneously (and potentially writing over each other's updates), the system uses a concurrency control mechanism

AIBS BBA-IB-III Sec-A

Page 12: Concurrency Control

AIBS BBA-IB-III Sec-A