0
0
DBMS Theoryknowledge~6 mins

Lock-based protocols in DBMS Theory - Full Explanation

Choose your learning style9 modes available
Introduction
When many users try to change data at the same time, conflicts can happen that cause errors or lost updates. Lock-based protocols help manage who can access data and when, so changes happen safely and correctly.
Explanation
Purpose of Lock-based Protocols
Lock-based protocols control access to data by allowing only one user or process to change or read data at a time. This prevents conflicts like two users trying to change the same data simultaneously. The protocol ensures data stays consistent and reliable.
Lock-based protocols prevent conflicts by controlling access to data.
Types of Locks
There are mainly two types of locks: shared locks and exclusive locks. Shared locks allow multiple users to read data but not change it. Exclusive locks allow only one user to read and change the data, blocking others until the lock is released.
Shared locks allow reading, exclusive locks allow reading and writing.
Two-Phase Locking (2PL)
Two-Phase Locking is a common lock-based protocol that has two parts: a growing phase where locks are acquired and a shrinking phase where locks are released. This method helps ensure that transactions happen in a way that keeps data consistent.
Two-Phase Locking ensures safe transaction order by separating lock acquisition and release.
Deadlocks
Deadlocks happen when two or more transactions wait forever for each other to release locks. Lock-based protocols need ways to detect and resolve deadlocks, such as aborting one transaction to free the locks.
Deadlocks occur when transactions block each other, requiring detection and resolution.
Real World Analogy

Imagine a library where people want to read or write notes in the same book. Shared locks are like allowing several people to read the book quietly, but exclusive locks are like one person writing notes, so others must wait. Two-phase locking is like first gathering all the books you need before starting to read or write, then putting them back only after finishing.

Purpose of Lock-based Protocols → Library rules that prevent people from fighting over the same book at the same time
Types of Locks → Shared reading spots for many readers and a reserved desk for one writer
Two-Phase Locking (2PL) → Collecting all needed books before starting work, then returning them after finishing
Deadlocks → Two people waiting for each other to finish using different books, causing a standstill
Diagram
Diagram
┌───────────────────────────────┐
│        Lock-based Protocols    │
├───────────────┬───────────────┤
│   Lock Types  │ Two-Phase     │
│               │ Locking (2PL) │
│ ┌───────────┐ │ ┌───────────┐ │
│ │ Shared    │ │ │ Growing   │ │
│ │ Lock      │ │ │ Phase     │ │
│ └───────────┘ │ ├───────────┤ │
│ ┌───────────┐ │ │ Shrinking │ │
│ │ Exclusive │ │ │ Phase     │ │
│ │ Lock      │ │ └───────────┘ │
│ └───────────┘ └───────────────┤
├───────────────┬───────────────┤
│   Deadlocks   │ Conflict       │
│               │ Resolution     │
└───────────────┴───────────────┘
This diagram shows the main parts of lock-based protocols: lock types, two-phase locking phases, and deadlock handling.
Key Facts
Shared LockAllows multiple transactions to read data but not write.
Exclusive LockAllows only one transaction to read and write data.
Two-Phase LockingA protocol with a growing phase to acquire locks and a shrinking phase to release them.
DeadlockA situation where transactions wait indefinitely for each other's locks.
Lock ReleaseThe process of freeing a lock so other transactions can access the data.
Code Example
DBMS Theory
class LockManager:
    def __init__(self):
        self.locks = {}

    def acquire_shared(self, data_item, transaction):
        if data_item not in self.locks:
            self.locks[data_item] = {'shared': set(), 'exclusive': None}
        lock = self.locks[data_item]
        if lock['exclusive'] is None or lock['exclusive'] == transaction:
            lock['shared'].add(transaction)
            return True
        return False

    def acquire_exclusive(self, data_item, transaction):
        if data_item not in self.locks:
            self.locks[data_item] = {'shared': set(), 'exclusive': None}
        lock = self.locks[data_item]
        if (lock['exclusive'] is None and len(lock['shared']) == 0) or lock['exclusive'] == transaction:
            lock['exclusive'] = transaction
            return True
        return False

    def release(self, data_item, transaction):
        if data_item in self.locks:
            lock = self.locks[data_item]
            lock['shared'].discard(transaction)
            if lock['exclusive'] == transaction:
                lock['exclusive'] = None

# Example usage
lm = LockManager()
print(lm.acquire_shared('x', 'T1'))  # True
print(lm.acquire_shared('x', 'T2'))  # True
print(lm.acquire_exclusive('x', 'T3'))  # False
lm.release('x', 'T1')
lm.release('x', 'T2')
print(lm.acquire_exclusive('x', 'T3'))  # True
OutputSuccess
Common Confusions
Believing shared locks allow data to be changed by multiple users at once.
Believing shared locks allow data to be changed by multiple users at once. Shared locks only allow reading; data changes require exclusive locks.
Thinking locks are held forever during a transaction.
Thinking locks are held forever during a transaction. In Two-Phase Locking, locks are released during the shrinking phase before the transaction ends.
Assuming deadlocks are errors that crash the system.
Assuming deadlocks are errors that crash the system. Deadlocks are normal in concurrent systems and are handled by detection and resolution methods.
Summary
Lock-based protocols manage data access to prevent conflicts during simultaneous transactions.
Shared locks allow multiple reads, while exclusive locks allow one write at a time.
Two-Phase Locking organizes lock acquisition and release to keep data consistent and avoid errors.