Question

2PL's rule says:

Upgrading of lock (from S(a) to X (a)) is allowed in growing phase.

But what if upgrading lies after the last lock and before the first unlock? Does it still count "in the growing phase"? For example, is the transaction below a 2PL?

begin
lock-S(X)
read(X)
lock-S(Y)
read(Y)
**upgrade(Y)**
write(Y)
unlock(X)
unlock(Y)
commit
Was it helpful?

Solution

It's allowed.

You can upgrade any locks during the growing phase, and downgrade any locks you want during the shrinking phase, and it's a valid 2PL. What you can't do is release/downgrade during the growing phase, or acquire/upgrade during the shrinking phase, because this breaks the phase ordering rule.

Note that even during the shrinking phase, downgrading breaks strict 2PL, because it releases an exclusive lock. More on this in a moment.

I think the reason why you may be asking is that your example transaction reads Y, then upgrades, then writes Y. You may be wondering what happens if some other transaction holds a shared lock on Y, and also wants to upgrade.

This is a deadlock situation, so one of them wins and the other transaction is aborted, or rolled back to at least the point where the shared lock on Y was acquired. The lock upgrade operation can and does cause aborts. Similarly, downgrading can cause cascading aborts, which is yet another reason why most database systems prefer strict locking.

Licensed under: CC-BY-SA with attribution
Not affiliated with cs.stackexchange
scroll top