CH-3 Concurrency Control New
CH-3 Concurrency Control New
2
Concurrency Control Techniques
Concurrency control techniques are those techniques used to ensure the isolation
guarantees serializability.
Some of the main techniques used to control concurrent execution of transactions are:-
Based on Locking
4
1. Concurrency Control Based on Locking
A Lock is a variable assigned to any data item in order to keep track of the status of
that data item.
Also lock is a variable associated with a data item that describes the status of the
item with respect to possible operations that can be applied to it.
Locks are used to synchronize(coordinate) the access to the data item.
A transaction acquires a lock prior to data access and the lock is released
(unlocked) when the transaction is completed.
Cont’d.
Lock and Unlock are atomic operation:-
Lock(X)- data item X is locked in behalf of the requesting transaction.
Unlock(X)- data item X is made available to all other transaction.
Most multiuser DBMSs automatically initiate and enforce locking
procedures.
All lock information is managed by a lock manager, which is responsible
for assigning, manage and controlling the locks used by the transactions.
Cont’d.
A database lock exists to prevent two or more database users from
performing any change on the same data item at the very same time.
Therefore, it is correct to interpret this technique as a means of
synchronizing access.
Lock Granularity
Preventing access to any row by transaction T2 while transaction T1 is using the table. T2 must
However, two transactions can access the same database as long as they access different tables.
But it also cause traffic jams when many transactions are waiting to access the same table.
Regardless of the simple level of locking, the DBMS may use different lock types(mode)
those are:-
1. Binary
2. Shared/Exclusive
1. Binary Lock
A binary lock is a variable capable of holding only 2 possible values.
Thus two states/values are : locked (1) or unlocked (0).
Every database operation requires that the affected object be locked.
If an object is locked by a transaction, no other transaction can use that
object.
If an object is unlocked, any transaction can lock the object for its use.
As a rule, a transaction must unlock the object after its termination.
Cont’d
Two operations, lock_item and unlock_item, are used with binary locking.
A transaction requests access to an item X by first issuing a lock_item (X) operation.
If LOCK(X) =1, the transaction is forced to wait.
If LOCK(X) =0, it is set to 1 and the transaction is allowed to access item X.
When the transaction completed using the item, it issues an unlock_item(X) operation,
which sets LOCK(X) back to 0 (unlocks the item) so that X may be accessed by other
transactions.
Shared locks allow several read transactions to read the same data item concurrently.
The exclusive lock is granted if and only if no other locks are held on the data item.
This condition is known as the mutual exclusive rule: only one transaction at a time
can own an exclusive lock on the same object.
Cont’d
Example 1:- Simple Shared/Exclusive Lock Transaction
Simple Lock(Shared/Exclusive)
T1 T2
read_lock_S(A); read_lock_X(A);
read_item(A); read_item(A);
unlock(A); write_item(A);
unlock(A);
read_lock_S(B);
read_item(B);
unlock(B);
Cont’d
Example 2 :- T1 transfer 100 birr to T2 and T2 wants to display(A+B)by using
simple Shared/Exclusive Lock
Simple Lock
T1 T2
read_lock_X(A); read_lock_S (A);
read_item(A); read_item(A);
A:=A-100; unlock(A);
write_item(A); read_lock_S (B);
unlock(A); read_item(B);
read_lock_X(B); unlock(B);
read_item(B); Display(A+B);
B:=B+100;
write_lock(B);
unlock(B);
Cont’d
Example:- Show the following transaction in Simple Lock Protocol
T1 T2
Read (P)
P=P+5
Write (P)
Read(P)
P=P*3
Write (P)
Read(Q)
Q=Q+5
W(Q)
Cont’d
Locks prevent data inconsistencies, but they can lead to two major
problems:
1. The resulting transaction schedule might not be serializable.
2. The schedule might create deadlocks. (A deadlock occurs when two
transactions wait indefinitely for each other to unlock data. )
To solve the above mentioned issues additional protocols concerning the
positioning of locking and unlocking operations are followed in every
transaction.
1. Two-Phase Locking (2PL) to Ensure Serializability
Two-phase locking defines how transactions acquire and relinquish (release) locks.
Two-phase locking guarantees serializability.
A transaction is said to follow the 2PL protocol if all locking operations precede the
first unlock operation of the transaction.
The transaction is divided into two phases:
Growing or Expanding phase (first phase) where new locks can be issued and
none can be released. Once all locks have been acquired, no locks are released.
Shrinking phase (second phase) where existing locks can be released and cannot
obtain any new lock. Once all locks are released and no locks are acquired.
Two-Phase Locking (2PL) to Ensure Serializability
Example
With 2PL
T1
read_lock_S(A);
read_item(A);
read_lock_X(B);
read_item(B);
B:=A+B;
unlock(A);
write_lock(B);
unlock(B):
Cont’d
T1
read_lock_S(A);
read_item(A);
read_lock_X(B);
unlock(A);
read_item(B);
write_item(B);
Commit;
unlock(B):
4. Rigorous 2PL
It is a more restrictive variation of Strict 2PL.
It requires that a transaction T does not release any of its locks (shared or
exclusive) until it commits or aborts.
It is easier to be implemented than the strict 2PL.
Cont’d
Now, let’s see the 2 schedule(A and B) below, check this schedule weather it can be locked using
2-PL or not, and if yes, show how and what class of 2-PL does your answer belongs to.
T1 T2 T1 T2
Lock-S(A) 1 Lock-S(A)
Read (A) 2 Read(A)
Lock-S(A) 3 Lock-S(A)
Read S(A) 4 Read(A)
Lock-X(B) 5 Lock-X(B)
Read X(B) 6 Read(B)
Write(B) 7 Write(B)
Commit 8 Commit
Unlock(B) Lock-X(B) 9 Unlock(A)
Read X(B) 10 Unlock(B)
Write(B) 11 Lock-X(B)
Commit 12 Read(B)
Unlock(B) 13 Write(B)
14 Commit
14 Unlock(A)
Observe that our locks are released after Commit operation so
15 Unlock(B)
this satisfies Schedule A Strict 2pl.2-PL protocol.
Deadlocks
A deadlock occurs when two transactions wait indefinitely for each other to
unlock data.
For example, a deadlock occurs when two transactions, T1 and T2, exist in the
following mode
• T1 = access data items X and need Y
• T2 = access data items Y and need X
Note that deadlocks are possible only when one of the transactions wants to
obtain an exclusive lock on a data item.
No deadlock condition can exist among shared locks.
Deadlocks
The idea for this scheme is to order the transactions based on their timestamps.
A schedule in which the transactions participate is then serializable, and the only equivalent
serial schedule permitted has the transactions in order of their timestamp values.
The algorithm must ensure that, for each item accessed by conflicting operations in the
schedule, the order in which the item is accessed does not violate the timestamp order:-
(TS(1) < TS(2)).
The TO algorithm associates with each database item X have two timestamp (TS) values:-
Those are:- RTS(X) and WTS(X)
Cont’d
1, RTS(X): The read timestamp of item X, which is the largest timestamp among
all the timestamps of transactions that have successfully read item X.
RTS(X) = TS(T), where T is the last transaction that has read X successfully.
2, WTS(X): The write timestamp of item X, which is the largest of all the
timestamps of transactions that have successfully written item X.
If WTS(X) > TS(Ti) , then read(X) is rejected(roll back Ti) (as the TO rule is violated).
[then a younger transaction has already written to the data item so abort and roll-back T and
reject the operation or Ti is an older Transaction then last Transaction that write the value of
X will request fail ]
If TS(Ti) >= WTS(X) ,then execute read(X) of T and update RTS(X) to the largest of
[then a younger/last transaction has already read/write the data item so abort and
roll-back T and reject the operation.]
If TS(T) >= RTS(X) and TS(Ti) >=WTS(X), then execute write(X) of T and
update WTS(X) = TS(Ti).
Example 1
Apply Basic TO Algorithm by using Two Case of Timestamp Algorithm
T1 T2 T3 R(A) 0>100 F
TS(T1)=100 TS(T2)=200 TS(T3)=300
Read(A) 0,100
Read(B) R(B) 0>200 F
Write (C) 0,200
Read(B) W(C) 0>100
0>100F
A B C Read(C) 0 to 100
RTS 0 0 0 Write (B)
100 200 100 Write(A)
300
WTS 0 0 0
300 R 100
Basic TO Algorithm Example 2
Two transactions T1 and T2. Initially RTS=0 and WTS=0 for data items X, Y
Timestamps are as follows: TS(T1)=10 and TS(T2)=20
T1 T2
TS(T1)=10 TS(T2)=20
1. A1 = Read(X)
2. A1 = A1 – k
3. Write(X, A1)
1. A1 = Read(X)
2. A1 = A1 * 1.01
3. Write(X, A1)
4. A2 = Read(Y)
5. A2 = A2 + k
6. Write(Y, A2)
4. A2 = Read(Y)
5. A2 = A2 * 1.01
6. Write(Y, A2)
RTS(X) : 20 4. A2 = Read(Y)
WTS(X) : 20 5. A2 = A2 + k
RTS(Y) : 10 6. Write(Y, A2)
WTS(Y) : 10 4. A2 = Read(Y) RTS(X) : 20
5. A2 = A2 * 1.01 WTS(X) : 20
6. Write(Y, A2) RTS(Y) : 20
WTS(Y) : 20
The schedule given above is serializable
Example 3
• Discuss whether the following schedule is serializable or not? Justify your answer using
Basic TO Algorithm.
T1 T2
TS(T1)=10 TS(T2)=20
1. A1 = Read(X)
2. A1 = A1 – k
3. Write(X, A1)
1. A1 = Read(X)
2. A1 = A1* 1.01
3. Write(X, A1)
4. A2 = Read(Y)
5. A2 = A2 * 1.01
6. Write(Y, A2)
4. A2 = Read(Y)
5. A2 = A2 + k
6. Write(Y, A2)
Cont’d
Strict Schedule: A transaction can neither read nor write an uncommitted data
item X.
Strict Timestamp Ordering: Extend the accept cases of the Basic Timestamp
Ordering algorithm by adding the requirement that a commit occurs before T
proceeds with its operation. i.e.,
Strict Timestamp Ordering
If WTS(X) < TS(Ti), then delay T until the transaction T’ that wrote X has
terminated (committed or aborted).
Strict Timestamp Ordering
Case 2: Transaction T issues a write(X) operation:
If RTS(X) <= TS(T) and WTS(X) <= TS(T), then delay T until the
transaction T’ that wrote and read X has terminated (committed or aborted).
Concurrency Control
58
3. Multi Version Concurrency Control
(MVCC)
When a MVCC database needs to update an item of data, it will not
overwrite the old data with new data, but instead mark the old data as
obsolete/outdated and add the newer version elsewhere.
Thus there are multiple versions stored, but only one is the latest.
This approach maintains a number of versions of a data item and allocates
the right version to a read operation of a transaction.
Cont’d
Unlike other mechanisms a read operation in this mechanism is never rejected.
When any transaction writes an item it writes a new version and the old version
of the item is retained.
Disadvantage
Assume X1, X2, …, Xn are the version of a data item X created by a write operation of
transactions.
With each Xi a RTS (read timestamp) and a WTS (write timestamp) are associated.
Basic Idea: Works much like Basic TO with the difference that instead of WTS(X) and
RTS(X) we now utilize the highest WTS(Xi) and highest RTS(Xi) respectively.
WTS(Xk+1) = TS(T).
RTS(Xk+1) = TS(T).
If transaction T issues read(X), find the version i of X that has the highest
WTS(Xi) of all versions of X that is also less than or equal to TS(T),
By Using Optimistic
65
4. Optimistic Concurrency Control
ordering
In optimistic concurrency control techniques no checking is done while the
transaction is executing.
1. Read & Execution phase: A transaction can read values of committed data
items from the database. However, updates are applied only to local copies
(versions) of the data items kept in the transaction workspace.
If the validation phase is successful, the transaction updates are applied to the database;
Note:
The techniques are called optimistic because they assume that little interference will
occur and there is no need to do checking during transaction execution.
Group Assignment
Group No Algorithms
Artificial Neural Network
Group 1
Decision Tree
Group 2
Logistic Regression,
Group 5
Data Warehousing
You should have to select your interested domain and apply classification algorithms.
Select appropriate Data mining tools (like WEKA,MATLAB,PYTHON) to design your model.
Deadline: - 15/05/2016 EC
Thank you!