Lock in transaction management | concurrency control
WHAT IS LOCK?
A 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.Generally,there is one lock for each data item in the database.Locks are used as a means of synchronizing the access by concurrent transactions to the database items.
WHY WE NEED LOCKING TECHNIQUES?
When the transactions are running concurrently the various locking methods are used to ensure serializability of the schedule i.e.to make the non-serial schedule produce the same result as that of a serial schedule.
LOCKING TECHNIQUES FOR CONCURRENCY CONTROL- The various techniques of concurrency control are:-
1.)Binary Locks-A Binary lock has two states or values:Locked and Unlocked.Represented as LOCK( ) and UNLOCK( ).
EG-Suppose there is a data item A and there is two transaction T1 and T2.If transaction T1 locks data item A (LOCK(A))then it is set to 1 and the other transaction(T2) is forced to wait.When the transaction is through using the item,it issues an UNLOCK(A) operation ,which sets it to 0 so that (A) may be accessed by other transaction.Hence a binary lock enforces mutual exclusion on the data item.We use binary locks to avoid Lost Update Problem.
T1 T2
(Set to 1) LOCK(A) (wait)
Read A
A=A+200
Write(A)
(set to 0) UNLOCK(A)
LOCK(A)
Read A
A=A+300
Write(A)
UNLOCK(A)
DISADVANTAGES OF BINARY LOCK:- It is too restrictive for database items.So, binary locking system cannot be used for practical purpose.
2.)SHARE/EXCLUSIVE(READ/WRITE LOCK)-Share lock can be used for read purpose only.However,if a transaction is to write an item A,it must have exclusive access to A.Share lock is represented as (LOCK S(A)) and exclusive lock is represented as (LOCK X (A)).Also known as read/wrie lock. EG-
T1
LOCK X(SUM) (exclusive lock on data item SUM)
SUM=
LOCK S(A) (Share lock on data item A)
Read A
SUM=SUM+A
UNLOCK(A)
LOCK S(B) (Share lock on data item B)
Read B
SUM=SUM+B
Write(SUM)
UNLOCK(B)
UNLOCK(SUM)
0 comments:
Post a Comment