Faculty of Engineering & Technology(Co-Edu)
Department of
ARTIFICIAL INTELLIGENCE & DATA SCIENCE
[Link]. 3rd Semester
ADBMS
Faculty: Dr. Savita Patil
TOPIC : 2 PHASE COMMIT AND
DEADLOCK RECOVERY
By
AJAY KATTIMANI
USN : SG22ADS001
WHAT IS TWO PHASE COMMIT
In a distributed database system, the Two-Phase Commit (2PC) protocol is a
fundamental mechanism used to ensure transactional consistency across multiple
database nodes. Here's an explanation of how the 2PC works within the context
of a distributed database in a DBMS:
1. Transaction Initiation:
2. Phase 1: Prepare Phase:
3. Phase 2: Commit or Abort Phase:
Case 1: Commit
Case 2: Abort
1
1. Transaction Initiation:
When a transaction begins on a distributed database, one of the nodes
(typically designated as the coordinator) is responsible for managing the
transaction.
The coordinator initiates the transaction and communicates with all
participating nodes (also known as participants) involved in the transaction.
2. Phase 1: Prepare Phase:
In the first phase of the two-phase commit protocol, the coordinator sends a "prepare
to commit" message to all participating nodes.
If a participant can accommodate the transaction, it replies to the coordinator with an
acknowledgment (YES). If any participant encounters a problem, it replies with a
refusal (NO).
2
3. Phase 2: Commit or Abort Phase:
Based on the responses received during the prepare phase, the coordinator
makes a decision to either commit or abort the transaction.
If all participants responded with an acknowledgment (YES), indicating
that they are ready to commit, the coordinator sends a "commit" message
to all participants.
If any participant responded with a refusal (NO), indicating that they
cannot proceed with the transaction, the coordinator sends an "abort"
message to all participants.
3
CASE 1: COMMIT
Coordinator Participants
Request-to-Prepare
Prepared
Commit
Done
4
Case 2: Abort
Coordinator Participants
Request-to-Prepare
No
Abort
Done
5
WHAT IS DEADLOCK
A deadlock is a situation in a computer system where two or more processes
are unable to proceed because each is waiting for the other to release a
resource, such as a lock or a piece of memory, before it can continue.
Deadlocks are common in concurrent or distributed systems where multiple
processes compete for shared resources.
6
DEADLOCK RECOVERY THROUGH 2 PHASE COMMIT
Detect Deadlock: First, the system needs to detect the presence of a deadlock. This
can be done through various deadlock detection algorithms that monitor resource
allocation and process states to identify circular wait conditions. (ex: Banker’s
Algorithm)
Identify Transactions: Once a deadlock is detected, the system identifies the
transactions involved in the deadlock. These transactions are typically those that are
holding resources needed by other transactions in the deadlock cycle.
Initiate 2PC: The system then initiates the Two-Phase Commit protocol to
coordinate the aborting of transactions involved in the deadlock. This involves the
following steps:
Phase 1 - Prepare: The coordinator (e.g., a deadlock detection and
recovery manager) sends a message to all transactions involved in
the deadlock, requesting them to prepare for abort. Transactions
respond with an acknowledgment indicating their readiness to abort.
7
Phase 2 - Commit or Abort: If all transactions agree to abort
(i.e., they respond affirmatively during the prepare phase), the
coordinator sends a commit message instructing all involved
transactions to abort. Otherwise, if any transaction refuses to
abort, the coordinator aborts the entire operation and may resort
to other recovery mechanisms.
Release Resources: With the aborted transactions releasing their held resources,
other transactions waiting for these resources can proceed, breaking the deadlock
cycle.
Resume Operations: Once the deadlock is resolved, normal system operations
can resume, and transactions can continue their execution.
8
10