2: Locks Flashcards
What is a lock ?
A transaction can acquire a lock on a data item prior to accessing that item.
This guarantees exclusive use of that item, and the lock is released when the transaction is complete.
What is the point of a lock ?
Locks prevent other transactions from reading inconsistent data, as this cannot be guaranteed during a transaction.
Who manages locking procedures ?
DMBS lock manager initiates and enforces the procedures.
At which levels can lock granularity take place?
Database, table, page, row, attribute
What are the two lock types?
- Binary locks: 2 states (1 locked, 0 unlocked), has to unlock after termination
- Shared/ Exclusive locks: shared allows other transactions to read the object, exclusive only the chosen one.
What are the two main locking issues ?
Locks prevent data inconsistencies but can cause transactions to be:
- Non-serialisable
- Deadlocks
What can be used to solve locking issues ?
Two Phase Locking Protocol (2PL).
It uses a ‘growing phase’ to acquire all the locks w/o unlocking any that time, and then conversely uses a ‘shrinking time’ to release them without locking anything else in the meantime.
What are deadlocks ?
T1 accesses items X and Y whilst T2 accesses items Y and X and neither has unlocked each others’ required item.
May happen if one of the transactions wants an exclusive lock on an item.
Does not occur amongst shared locks.
What are the 3 ways to control deadlocks ?
- Deadlock Prevention: transaction requesting a new lock is aborted if there is possibility of deadlock. Abort rolls back the transaction and releases all of its locks that are being held at the time. (Wow, this like in the Flash)
- Deadlock Detection: DMBS tests for deadlocks and aborts one of transactions whilst allowing the other to continue
- Deadlock Avoidance: Transaction must obtain all locks before it can execute. This avoids rollback but may be slower due to having to acquire all locks that are needed.
Can deadlock occur when a transaction requests a shared lock on an item?
No – deadlock occurs on exclusive lock issues.
Which of the following can be used for deadlock detection?
I. Timeout II. Precedence Graph III. 2PL Protocol IV. Wait-For Graph V. Rolling back a transaction
I. Timeout
and
IV. Wait-For Graph