next up previous
Next: Incremental Sharing Up: No Title Previous: Locking

Two-Phase Locking

This approach, developed by Jim Gray, locks data and assumes that a transaction is divided into a growing phase, in which locks are only acquired, and a shrinking phase, in which locks are only released. The use of 2-phase locks is illustrated in Figure 4.

A transaction that tries to lock data that has been locked is forced to wait and may deadlock, as shown in Figure 5.

The above example illusrtates why 2PL ensures serializability. If a schedule is not serializable, transactions will deadlock if they try to execute a non serializable schedule. Recall that serializability required the transaction graph we created above to not have cycles. 2PL ensures that such cycles in the graph will lead to deadlocks rather than successful execution of the transactions. Let us try to prove this by contradiction. Suppose the transaction graph has cycles but there is no deadlock. If the graph has cycles then it means that we can find a pair of transactions. T1 and T2, such that T1 accesed an item, o1, after T2 accessed it, and T2 accessed an item, o2, before T accessed it. In 2PL we know that this means both transactions had locks to both items at some time. This means that one transaction had both locks before the other one had any lock. Let us say T1 had these locks before T2. As each lock serializes accesses to the item, this means T1 accessed both items before T2, which contradicts our claim that the transaction graph has cycles. Thus, cycles in the transaction graph result in cycles in cyclic locking.



Prasun Dewan
Thu Mar 10 10:57:47 EST 2005