Locking Flashcards
Locking
Used to enforce conflict serialisability.
Simple Lock Mechanism
Used to ensure that a transaction has to lock an item before it can access it, which are requested and granted by the scheduler.
Each item is locked at most one transaction at a time, and transactions wait until a lock can be granted.
These locks have to be released eventually.
This is SIMILAR in theory to what serialization does in terms of setting the isolation of the transaction.
Lock Denoted
Locking -> l_i(x)
Unlocking -> u_i(x)
Example of locking
l_1(x) would request the lock for transaction one for the item x.
u_1(x) would release this lock.
If there was a l_2(x) between these two, then transaction two will have to wait for transaction one to finish what it is doing.
Two Phase Locking
All lock operations in each transaction precede all unlocks.
Phase 1 is requesting locks, and phase 2 is unlocking.
So in other words, there cannot be an unlock which happens before a lock, even if they lock/unlock different things.
Deadlocks
Transactions may be forced to wait forever.
If we take the example above, if transaction 2 starts before transaction 1 can get to locking y, then neither can proceed forward since we are waiting for either of them to release their locks.
Deadlock Solution
Using more flexible locks.
Shared Locks (read-locks)
Requested by transactions to read an item x.
Granted to several transactions at the same time.
Operation : s-lock(x)
Shared lock on x is granted only if no other transactions hold the exclusive lock for x.
Exclusive Locks (write-locks)
Requested by transactions to write an item x.
Granted to at most one transaction at a time.
Operation : x-lock(x)
Shared lock on x is granted only if no other transactions hold ANY lock for x.
Upgrading Locks
A shared lock on item x can be upgraded later to an exclusive lock on x i order to be friendly to other transactions.
Deadlock Solution (Shared and exclusive locks)
Using shared and exclusive locks can still lead to deadlock. For example:
sl_1(x), r_1(x), sl_2(x), r_2(x)
If both transactions have xl_i(x) after them, then since it requires X having no locks, we are stuck in deadlock.
Update Lock
Requested by transactions to read (not write) an item, and may be upgraded to exclusive locks.
Granted to at most on transaction at a time.
Operation : u-lock(x)
Upgrade System
If you have a shared lock, you can upgrade to update lock, but not vice versa (you cannot upgrade from update to shared lock).
We cannot use exclusive locks at all from either shared locks or upgrade locks.
You can go from shared lock to shared lock, but not update lock to update lock.
Locking too broadly
Low overhead (less locks to be stored, aka less information), but less degree of concurrency (since less locks may cause more concurrent schedules, and may cause delays).
Locking too finely
High overhead since we have more locks, but high degree of concurrent with no delays.