Sub topic 15
Distributed Transactions and Two-Phase Commit in Oracle
Assume you know basic SQL transactions, COMMIT, ROLLBACK, and database links. This chapter explains what changes when one transaction updates more than one Oracle database.
What is a distributed transaction?
A distributed transaction is a transaction that uses resources in two or more databases. The databases may be on different servers. Oracle connects them with a database link.
For example, a local order database might update an order while a remote inventory database reduces stock. Both changes belong to one business operation. The application should not commit one change while losing the other.
-- The database link must already exist.
UPDATE orders
SET status = 'SHIPPED'
WHERE order_id = 1001;
UPDATE inventory@warehouse_link
SET quantity = quantity - 1
WHERE product_id = 501;
COMMIT;The @warehouse_link suffix tells Oracle to execute the statement in the remote database. Because both statements belong to the same open transaction, Oracle coordinates their outcome.
A distributed transaction has:
- A coordinator, usually the database where the transaction began.
- Participants, which are the databases that perform work.
- A global transaction identifier used to track the work across databases.
Two-phase commit
Oracle normally uses two-phase commit (2PC) to finish a distributed transaction safely. The protocol has a prepare phase and a commit phase.
Phase 1: Prepare
The coordinator asks every participant whether it can commit. Each participant:
- Finishes its statement processing.
- Writes the required transaction information to durable storage.
- Locks affected rows.
- Replies
READYor reports failure.
A participant that replies READY promises that it can commit later. It cannot independently decide to roll back.
Phase 2: Commit or rollback
If every participant is ready, the coordinator sends COMMIT. Each participant makes its changes permanent and releases its locks.
If any participant cannot prepare, the coordinator sends ROLLBACK to all participants. The transaction then fails as a unit.
| Outcome | Coordinator action | Result |
|---|---|---|
| All participants prepare successfully | Send `COMMIT` | All changes become permanent |
| A participant fails before prepare completes | Send `ROLLBACK` | All prepared work is undone |
| Network or server failure during completion | Recover the global transaction | A participant may remain in doubt temporarily |
In-doubt transactions
An in-doubt transaction is a distributed transaction whose final outcome is temporarily unknown because communication failed during 2PC. It can hold locks and delay other work.
A database administrator investigates these transactions using Oracle's distributed transaction views, such as DBA_2PC_PENDING, and coordinates recovery with the other database or service owner. Do not manually force a decision without confirming the global outcome. Forcing the wrong result can create inconsistent business data.
Practical design rules
Keep a distributed transaction short. Perform validation before changing remote data, avoid user interaction while it is open, and commit one complete business operation at a time.
Use local transactions when possible. A distributed transaction adds network delay, coordination work, and more failure cases. If the business process can tolerate delayed synchronization, an outbox or message-based design may be simpler than 2PC.
Remember that a local ROLLBACK before the final commit can undo the transaction's work. After Oracle has completed the global commit, a later local rollback cannot reverse it.
Exercise
Why does Oracle ask every participant to prepare before sending COMMIT?
Show answer
Preparation lets each participant durably promise that it can commit. The coordinator can then choose one outcome for all participants instead of allowing one database to commit while another rolls back.