Two-Phase Locking
IT 위키
Two-Phase Locking (2PL) is a concurrency control protocol used in database systems to ensure serializability of transactions. It operates in two distinct phases where locks are acquired and released in a specific order, preventing conflicts between concurrent transactions.
Key Concepts[편집 | 원본 편집]
- Locking Protocol: Transactions acquire locks on data items before accessing them and release locks when no longer needed.
- Serializability: 2PL guarantees that the resulting transaction schedule is equivalent to a serial execution.
- Phases of 2PL: The protocol divides transaction execution into two phases:
- Growing Phase: Locks are acquired but not released.
- Shrinking Phase: Locks are released but not acquired.
Phases of Two-Phase Locking[편집 | 원본 편집]
- Growing Phase:
- The transaction requests and acquires locks as needed.
- No locks are released in this phase.
- Shrinking Phase:
- The transaction releases locks as operations complete.
- No new locks are acquired after the first lock is released.
Example of Two-Phase Locking[편집 | 원본 편집]
Consider two transactions, T1 and T2, accessing a shared data item:
Step | Transaction T1 | Transaction T2 | Explanation |
---|---|---|---|
1 | BEGIN TRANSACTION | BEGIN TRANSACTION | Both transactions start. |
2 | READ(A) | - | T1 acquires a shared lock on A and reads its value. |
3 | - | WRITE(A) | T2 waits because T1 holds a shared lock on A. |
4 | WRITE(A) | - | T1 upgrades to an exclusive lock and writes to A. |
5 | COMMIT | - | T1 releases its locks, allowing T2 to proceed. |
6 | - | WRITE(A) | T2 acquires an exclusive lock and writes to A. |
Variants of Two-Phase Locking[편집 | 원본 편집]
Two-Phase Locking has several variants to handle specific requirements:
- Strict 2PL:
- Locks are held until the transaction commits or aborts.
- Prevents cascading rollbacks and ensures recoverability.
- Rigorous 2PL:
- All locks are held until the transaction commits.
- Provides the strictest guarantees and simplifies recovery.
- Conservative 2PL:
- Transactions acquire all required locks before execution begins.
- Prevents deadlocks but may reduce concurrency.
Deadlock in Two-Phase Locking[편집 | 원본 편집]
2PL can lead to deadlocks when two or more transactions wait indefinitely for locks held by each other. Common strategies to handle deadlocks include:
- Deadlock Detection: Use a wait-for graph to identify cycles and abort one of the transactions.
- Deadlock Prevention: Enforce lock acquisition order to avoid circular waits.
- Timeouts: Abort transactions that wait too long for a lock.
Advantages[편집 | 원본 편집]
- Guarantees Serializability: Ensures that transactions execute in a serializable manner.
- Simplicity: Easy to understand and implement compared to other protocols.
- Widely Used: Forms the basis for many database concurrency control mechanisms.
Limitations[편집 | 원본 편집]
- Deadlock Risk: Can result in deadlocks if not managed properly.
- Reduced Concurrency: Lock contention may lower system performance in high-concurrency environments.
- Long Lock Holding: Variants like Strict 2PL can increase the duration of lock holding, impacting throughput.
Applications[편집 | 원본 편집]
Two-Phase Locking is commonly used in:
- Transactional Databases: To ensure consistency in banking, e-commerce, and other critical systems.
- Distributed Systems: Ensures consistency across nodes in distributed database architectures.
- Enterprise Systems: Used in systems where serializability is a strict requirement.