# Does upgrading after all locks and before all unlocks violate 2PL?

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)
lock-S(Y)
write(Y)
unlock(X)
unlock(Y)
commit

• You have also posted this on Stack Overflow. Please don't cross-post! Oct 16 '20 at 4:17

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.

• Thanks! That really clarified all my doubt.
– soma
Oct 16 '20 at 6:19