LOCKING AND TIME STAMPS Flashcards
What is locking?
A procedure used to control concurrent access to data. When one transaction is accessing the database, a lock may deny access to other transactions to prevent incorrect results.
In this method, a scheduler uses the locks to ensure serializability
How does a transaction obtain a lock?
Transactions must request and release locks in addition to reading and writing database elements.
A transaction obtains locks on the database elements it accesses , to prevent other transactions from accessing these elements, at roughly the same time, thereby incurring the risk of un-serializability .
What are the two types of locks?
- Shared lock If a transaction has a shared lock on a data item, it can read the item but not update it. Since read operations cannot conflict, it is permissible for more than one transaction to hold shared locks simultaneously on the same item
- Exclusive lock If a transaction has an exclusive lock on a data item, it can both read and update the item. As long as a transaction holds the exclusive lock on the item, no other transactions can read or update that data item
How are locks used?
Any transaction that needs to access a data item must first lock the item, requesting a shared lock for read only access or an exclusive lock for both read and write access.
If the item is not already locked by another transaction, the lock will be granted.
If the item is currently locked, the DBMS determines whether the request is compatible with the existing lock.
If a shared lock is requested on an item that already has a shared lock on it, the request will be granted; otherwise, the transaction must wait until the existing lock is released
When is a lock released
A transaction continues to hold a lock until it explicitly releases it either during execution or when it terminates (aborts or commits). It is only when the exclusive lock has been released that the effects of the write operation will be made visible to other transactions
Describe an incorrect locking Schedule
*See doc for pic
What is two-phase locking (2PL)?
A protocol concerning the positioning of the lock and unlock operations in every transaction in order to guarantee conflict serializability.
What is conflict serializability?
A schedule is called conflict serializable if it can be transformed into a serial schedule by swapping non-conflicting operations.
What are conflicting operaions?
Conflicting operations: Two operations are said to be conflicting if all conditions satisfy:
They belong to different transactions
They operate on the same data item
At Least one of them is a write operation
What are the two phases of a transaction according to 2PL?
- Growing phase, in which it acquires all the locks needed but cannot release any locks.
- Shrinking phase, in which it releases its locks but cannot acquire any new locks.
What are the rules of 2PL?
- A transaction must acquire a lock on an item before operating on the item. The lock maybe read or write, depending on the type of access needed.
- Once the transaction releases a lock, it can never acquire any new locks.
Describe how 2PL can prevent lost update problem, uncommitted dependency problem and inconsistent analysis problem using 2PL
*See phone for pics
What is a deadlock?
several transactions are forced by the scheduler to wait for a lock held by another transaction
Results when two (or more) transactions are each waiting for locks to be released that are held by the other.
How can one break deadlock?
Abort one or more of the transactions usually involving undoing all the changes made by aborted transaction(s)
Describe deadlock prevention and how is it carried out?
DBMS looks ahead to see if transaction would cause deadlock and never allows deadlock to occur.
Could order transactions using transaction timestamps:
Wait-Die - only an older transaction can wait for younger one, otherwise transaction is aborted (dies) and restarted with same timestamp.
Wound-Wait - only a younger transaction can wait for an older one. If older transaction requests lock held by younger one, younger one is aborted (wounded).