ContentContent
Concurrency ProblemsConcurrency Problems
Concurrency ControlConcurrency Control
Concurrency Control ApproachesConcurrency Control Approaches
Concurrency ProblemsConcurrency Problems
Data integrity and consistency problem may Data integrity and consistency problem may arise when several transactions are processed arise when several transactions are processed simultaneously (multi-user DB environment)simultaneously (multi-user DB environment)
Example: Ali and Siti have a joint savings Example: Ali and Siti have a joint savings account in a bank and each has been issued an account in a bank and each has been issued an ATM card. It may happen that both Ali and Siti ATM card. It may happen that both Ali and Siti withdraw money at the same time in 2 different withdraw money at the same time in 2 different locations.locations.
Concurrency Problems (cont…)Concurrency Problems (cont…)TimeTime Ali’s TransactionAli’s Transaction Siti’s TransactionSiti’s Transaction
11 Read account balanceRead account balance
(Balance = RM1000)(Balance = RM1000)
22 Read account balanceRead account balance
(Balance = RM1000)(Balance = RM1000)
33 Withdraw RM600Withdraw RM600
(Balance = RM400)(Balance = RM400)
Write Balance = RM400Write Balance = RM400
44 Withdraw RM700Withdraw RM700
(Insufficient Funds!)(Insufficient Funds!)
Concurrency ControlConcurrency Control
This type of problems are caused by lack This type of problems are caused by lack of coordination of the two transactions.of coordination of the two transactions.
Maybe the DBMS does not provide Maybe the DBMS does not provide concurrency controlconcurrency control..
Concurrency control refers to the Concurrency control refers to the coordination of execution of multiple coordination of execution of multiple transactions in a multi-user DB transactions in a multi-user DB environment.environment.
Concurrency ControlConcurrency Control
3 problems associated with concurrent 3 problems associated with concurrent processing :processing :
1.1. Lost updatesLost updates
2.2. Uncommitted dataUncommitted data
3.3. Inconsistent retrievalsInconsistent retrievals
Lost UpdatesLost Updates
Scenario Scenario
Assume that customer balance (Bal) is 500. And Assume that customer balance (Bal) is 500. And 2 transaction T1 and T2 attempt to update the 2 transaction T1 and T2 attempt to update the balance at the same time.balance at the same time.
TransactionTransaction ActionAction ComputationComputation
T1T1 Deposit 200Deposit 200 Bal = 500+200 Bal = 500+200
(Bal = 700)(Bal = 700)
T2T2 Withdraw 100Withdraw 100 Bal = 700-100 Bal = 700-100
(Bal = 600)(Bal = 600)
The serial execution of these transaction The serial execution of these transaction yields the correct results (Bal = 600)yields the correct results (Bal = 600)
Transaction requires several steps such Transaction requires several steps such as reading, modifying and writing. And it as reading, modifying and writing. And it must be in correct sequence. must be in correct sequence.
See the correct sequence for the previous See the correct sequence for the previous transactions.transactions.
Lost Updates (cont…)Lost Updates (cont…)
Lost Updates (cont…)Lost Updates (cont…)
TimeTime Trans.Trans. StepStep Stored ValueStored Value
11 T1T1 Read BalanceRead Balance 500500
22 T1T1 Balance=500+200Balance=500+200
33 T1T1 Write BalanceWrite Balance 700700
44 T2T2 Read BalanceRead Balance 700700
55 T2T2 Balance=700-100Balance=700-100
66 T2T2 Write BalanceWrite Balance 600600
Lost Updates (cont…)Lost Updates (cont…)
TimeTime Trans.Trans. StepStep Stored ValueStored Value
11 T1T1 Read BalanceRead Balance 500500
22 T2T2 Read BalanceRead Balance 500500
33 T1T1 Balance=500+200Balance=500+200 700700
44 T2T2 Balance=500-100Balance=500-100 400400
55 T1T1 Write Balance Write Balance (Lost Update)(Lost Update) 700700
66 T2T2 Write BalanceWrite Balance 400400
The scheduler below shows incorrect sequence leading to lost updates
Uncommitted DataUncommitted Data
Data are not committed when 2 Data are not committed when 2 transaction T1 and T2 are executed transaction T1 and T2 are executed concurrently, and concurrently, and T1 is rolled back after T2 T1 is rolled back after T2 has already accessed the uncommitted has already accessed the uncommitted datadata
This violates the isolation property of This violates the isolation property of transactiontransaction
Uncommitted Data (cont…)Uncommitted Data (cont…)
TransactionTransaction ActionAction ComputationComputation
T1T1 Deposit 200Deposit 200 Bal = 500+200 Bal = 500+200
(Rolled back)(Rolled back)
T2T2 Withdraw 100Withdraw 100 Bal = 500-100 Bal = 500-100
(Bal = 400)(Bal = 400)
This serial execution yields the correct results Balance = 400
Uncommitted Data (cont…)Uncommitted Data (cont…)TimeTime Trans.Trans. StepStep Stored ValueStored Value
11 T1T1 Read BalanceRead Balance 500500
22 T1T1 Balance=500+200Balance=500+200
33 T1T1 Write BalanceWrite Balance 700700
44 T1T1 Roll backRoll back 500500
55 T2T2 Read BalanceRead Balance 500500
66 T2T2 Balance=500-100Balance=500-100
77 T2T2 Write BalanceWrite Balance 400400
The scheduler below shows incorrect sequence leading to lost updates
TimeTime Trans.Trans. StepStep Stored ValueStored Value
11 T1T1 Read BalanceRead Balance 500500
22 T1T1 Balance=500+200Balance=500+200
33 T1T1 Write BalanceWrite Balance 700700
44 T2T2 Read Balance Read Balance (Uncommitted data)(Uncommitted data)
55 T1T1 Roll backRoll back 500500
66 T2T2 Balance=700-100Balance=700-100
77 T2T2 Write Balance (Lost Write Balance (Lost update)update)
600600
Inconsistent RetrievalInconsistent Retrieval
Occurs when a transaction calculates an Occurs when a transaction calculates an aggregate or summary function (e.g SUM) aggregate or summary function (e.g SUM) over a set of data, which the other over a set of data, which the other transactions are updatingtransactions are updating
The inconsistency happens because the The inconsistency happens because the transaction may read some data before transaction may read some data before they are changed and read other data they are changed and read other data after they are changedafter they are changed
Locking – If one user is updating the data, Locking – If one user is updating the data, all the other users denied access to the all the other users denied access to the same datasame data
Time stamping – a unique global time Time stamping – a unique global time stamp is assigned to each transactionstamp is assigned to each transaction
Concurrency Control ApproachesConcurrency Control Approaches
LockingLocking
A lock is a mechanism to control concurrent access to a A lock is a mechanism to control concurrent access to a data itemdata itemData items can be locked in two modes :Data items can be locked in two modes :
11. . exclusiveexclusive (X) mode (X) mode. Data item can be both read as . Data item can be both read as well as written. X-lock is requested using well as written. X-lock is requested using lock-X lock-X instruction.instruction.
22. . sharedshared (S) mode (S) mode. Data item can only be read. S-lock . Data item can only be read. S-lock is requested using is requested using lock-S lock-S instruction. instruction.
Lock requests are made to concurrency-control Lock requests are made to concurrency-control manager. Transaction can proceed only after request is manager. Transaction can proceed only after request is granted.granted.
Locking (cont…)Locking (cont…)
Lock-compatibility matrixLock-compatibility matrix
A transaction may be granted a lock on A transaction may be granted a lock on an item if the requested lock is an item if the requested lock is compatible with locks already held on compatible with locks already held on the item by other transactionsthe item by other transactions
Locking (cont…)Locking (cont…)
Any number of transactions can hold shared Any number of transactions can hold shared locks on an item, but if any transaction holds an locks on an item, but if any transaction holds an exclusive on the item no other transaction may exclusive on the item no other transaction may hold any lock on the item.hold any lock on the item.
If a lock cannot be granted, the requesting If a lock cannot be granted, the requesting transaction is made to wait till all incompatible transaction is made to wait till all incompatible locks held by other transactions have been locks held by other transactions have been released. The lock is then granted.released. The lock is then granted.
Example of a transaction performing locking:
T2: lock-S(A); read (A); unlock(A); lock-S(B); read (B); unlock(B); display(A+B)
Locking as above is not sufficient to guarantee serializability — if A and B get updated in-between the read of A and B, the displayed sum would be wrong.
DeadlockDeadlockConsider the partial scheduleConsider the partial schedule
Neither Neither TT33 nor nor TT44 can make progress — can make progress — executing executing lock-Slock-S(B)(B) causes causes TT44 to wait for to wait for TT33 to to release its lock on release its lock on BB, while executing , while executing lock-lock-XX(A)(A) causes causes TT33 to wait for to wait for TT44 to release its lock to release its lock on on AA..
Deadlock (cont…)Deadlock (cont…)
Such a situation is called a Such a situation is called a deadlockdeadlock. . To handle a deadlock one of To handle a deadlock one of TT33 or or TT44 must be must be
rolled back rolled back and its locks released.and its locks released.
Methods to control deadlocksMethods to control deadlocks
Deadlock preventionDeadlock prevention - transacation - transacation requesting a new lock is aborted if there is requesting a new lock is aborted if there is a possibility that it might cause a deadlock a possibility that it might cause a deadlock to occurto occur
Methods to control deadlocksMethods to control deadlocks
Deadlock detection - Deadlock detection - DBMS periodically DBMS periodically checks the DB for any deadlocks. If exist it checks the DB for any deadlocks. If exist it aborts one of the transactionaborts one of the transaction
Deadlock recoveryDeadlock recovery - requires each - requires each transaction locks all its data items before it transaction locks all its data items before it begins executionbegins execution
Time stampingTime stamping
Each transaction is issued a timestamp Each transaction is issued a timestamp when it enters the system. If an old when it enters the system. If an old transaction transaction TTii has time-stamp TS( has time-stamp TS(TTii), a ), a
new transaction new transaction TTjj is assigned time-stamp is assigned time-stamp
TS(TS(TTjj) such that TS() such that TS(TTii) <TS() <TS(TTjj). ).
The protocol manages concurrent The protocol manages concurrent execution such that the time-stamps execution such that the time-stamps determine the serializability order.determine the serializability order.