Two Phase Commit Protocol (2PC) ■ Execution of the protocol is initiated by the coordinator after the last step of the transaction has been reached. The protocol involves all the local sites at which the transaction executed Protocol has two phases Let T be a transaction initiated at site S,and let the transaction coordinator at S;be C Database System Concepts-7th Edition 23.7 ©Silberscha乜,Korth and Sudarshan
Database System Concepts - 7 23.7 ©Silberschatz, Korth and Sudarshan th Edition Two Phase Commit Protocol (2PC) ▪ Execution of the protocol is initiated by the coordinator after the last step of the transaction has been reached. ▪ The protocol involves all the local sites at which the transaction executed ▪ Protocol has two phases ▪ Let T be a transaction initiated at site Si , and let the transaction coordinator at Si be Ci
Phase 1:Obtaining a Decision Coordinator asks all participants to prepare to commit transaction T. C adds the records <prepare T>to the log and forces log to stable storage sends prepare T messages to all sites at which T executed Upon receiving message,transaction manager at site determines if it can commit the transaction if not,add a record <no T>to the log and send abort T message to C if the transaction can be committed,then: add the record <ready 7>to the log force all records for Tto stable storage send ready 7 message to C Transaction is now in ready state at the site Database System Concepts-7th Edition 23.8 ©Silberscha乜,Korth and Sudarshan
Database System Concepts - 7 23.8 ©Silberschatz, Korth and Sudarshan th Edition Phase 1: Obtaining a Decision ▪ Coordinator asks all participants to prepare to commit transaction Ti . • Ci adds the records <prepare T> to the log and forces log to stable storage • sends prepare T messages to all sites at which T executed ▪ Upon receiving message, transaction manager at site determines if it can commit the transaction • if not, add a record <no T> to the log and send abort T message to Ci • if the transaction can be committed, then: ▪ add the record <ready T> to the log ▪ force all records for T to stable storage ▪ send ready T message to Ci Transaction is now in ready state at the site
Phase 2:Recording the Decision 7 can be committed of C,received a ready 7 message from all the participating sites:otherwise 7 must be aborted. Coordinator adds a decision record,<commit T>or <abort T>,to the log and forces record onto stable storage.Once the record stable storage it is irrevocable (even if failures occur) Coordinator sends a message to each participant informing it of the decision(commit or abort) Participants take appropriate action locally Database System Concepts-7th Edition 23.9 ©Silberscha乜,Korth and Sudarshan
Database System Concepts - 7 23.9 ©Silberschatz, Korth and Sudarshan th Edition Phase 2: Recording the Decision ▪ T can be committed of Ci received a ready T message from all the participating sites: otherwise T must be aborted. ▪ Coordinator adds a decision record, <commit T> or <abort T>, to the log and forces record onto stable storage. Once the record stable storage it is irrevocable (even if failures occur) ▪ Coordinator sends a message to each participant informing it of the decision (commit or abort) ▪ Participants take appropriate action locally
Two-Phase Commit Protocol Coordinator Node N1 Node N2 Force log record <prepare T> Send message prepare T Force log record Force log record <ready T> ready T> Send message Send message ready T ready T Force log record commit T> Send message commit T Force log record Force log record <commit T> commit T> Database System Concepts-7th Edition 23.10 @Silberschatz,Korth and Sudarshan
Database System Concepts - 7 23.10 ©Silberschatz, Korth and Sudarshan th Edition Two-Phase Commit Protocol
Handling of Failures-Site Failure When site Sk recovers,it examines its log to determine the fate of transactions active at the time of the failure. ■Log contain<commit 7下record:site executes redo(T刀 Log contains <abort T>record:site executes undo(7) ■ Log contains <ready T>record:site must consult C;to determine the fate of T. If T committed,redo() ·If T aborted,undo(刀 The log contains no control records concerning T implies that Sk failed before responding to the prepare T message from Ci since the failure of Sk precludes the sending of such a response C;must abort T ·Skmust execute undo(T刀 Database System Concepts-7th Edition 23.11 ©Silberscha乜,Korth and Sudarshan
Database System Concepts - 7 23.11 ©Silberschatz, Korth and Sudarshan th Edition Handling of Failures - Site Failure When site Sk recovers, it examines its log to determine the fate of transactions active at the time of the failure. ▪ Log contain <commit T> record: site executes redo (T) ▪ Log contains <abort T> record: site executes undo (T) ▪ Log contains <ready T> record: site must consult Ci to determine the fate of T. • If T committed, redo (T) • If T aborted, undo (T) ▪ The log contains no control records concerning T implies that Sk failed before responding to the prepare T message from Ci • since the failure of Sk precludes the sending of such a response Ci must abort T • Sk must execute undo (T)