140 likes | 372 Views
Enforcing Serializability By LOCKS. By Jignesh Borisa (Cs257 ID:111). LOCKS. request from transactions Lock Table
E N D
Enforcing Serializability By LOCKS By Jignesh Borisa (Cs257 ID:111)
LOCKS request from transactions Lock Table Serializable Schedule of actions Scheduling
Transaction obtains lock on the database elements. So it accesses to prevent other transactions from accessing these elements. In this scheme,there is only one kind of lock, which transactions must obtain on a database if they want to perform any operation on that element.
A scheduler that uses a lock table to help perform its job. Scheduler takes request from transactions and either allow them to operate on the database or defer them. Scheduler would forward a request if and only if its execution can not possibly lead to an inconsistent database state after all action commit or abort.
When a scheduler uses locks , transactions must request and release locks, in addition to reading and writing database elements. Consistency of Transactions: 1. A transaction can only read or write an element if it previously requested on that element and hasn’t yet released lock. 2.If transaction looks an element , it must later unlock that element.
Legality of Schedules: Locks must have their intended meaning: no two transactions may have locked the same element without one having first released the lock. l(x):Transaction T requests a lock on database element X. (x):Transaction T releases its locks on database elements X.
Example T1 T2 ------------------------------------------------------------------- l1(A);r1(A); l1(A);r2(A); A:=A+100; A=A*2; w1(A);u1(A); w2(A);u2(A); l1(B);r1(B); l2(B);r2(B); B:=B+100; B=B*2; w1(B);u1(B); w2(B);u2(B); T2’s action is performed after T1.
The Locking Scheduler • The job of scheduler based on locking to grant requests if and only if the request will result in a legal schedule. • For this, lock table which tells for every database element, the transaction, if any, that currently holds a locks on that element. • Lock( element, trasaction) consisting of pairs(X,T) .
Example T1 T2 A B ------------------------------------------------------------------------ l1(A);r1(A); 25 25 A:=A+100; w1(A);l1(B);u1(A); 125 l2(A);r2(A); A:=A*2; w2(A); 250 l2(B); Denied r1(B); B:=B+100; w1(B);u1(B); 150 l2(B);u2(A);r2(B); B:=B*2; w2(B);u2(B); 250
Two Phase Locking • Condition under which we can guarantee that a legal schedule of consistent transactions is conflict- serializable. • Also Called as 2PL. • The 2PL condition is: In every transaction , all lock requests precede all unlock requests.
Why Two Phase Locking Works • Each two phase locked transaction may be thought to execute in its entirety at the instant it issues its first unlock request. Instantaneously executs now locks required time
Conversion of two phase locked transactions to a conflict-equivalent serial schedule for schedule S. n , the number of transaction in S. Basis: If n=1,there is nothing to do; S is already a serial Schedule. Induction : S involves n transactions T1,T2…Tn Ti be the transaction with the first unlock action in the entire schedule S, say ui(x).
Consider some action of Ti,say wi(y). If wj(y), preceded wi(y) then action uj(y) and li(y) must intervene, in sequence of actions …wj(y);….;uj(y);…;li(y);…;wi(y);…. Since Ti is the first to unlock, ui(x) precedes uj(y) in S; that S might look like: ….;wj(Y);…;ui(x);…;uj(y);…;li(y);…;wj(y);… S can be written in the form ( Action of Ti)(Action of the other n-1 transaction)
Example on Risk of Deadlock T1 T2 A B __________________________________________________ l1(A);r1(A); 25 25 l2(B);r2(B); A:=A+100 B:=B*2 w1(A) 125 w2(B); 50 l1(B) Denied l2(A) Denied