Sub topic 15
Distributed transactions and two-phase commit (overview)
This chapter assumes you know how a single-database transaction works: COMMIT, ROLLBACK, savepoints, and the locks held while a transaction is open. It also assumes you can create and query a database link, or are willing to read the examples as sketches. Distributed transactions are rarely something you design on purpose. They usually appear when a transaction touches more than one database. The goal here is to recognize them, understand what Oracle does for you, and know what to do when something goes wrong.
What makes a transaction distributed
A distributed transaction is a transaction that changes data in two or more databases and still commits or rolls back as one unit. In Oracle, the usual way to reach a second database is a database link. A database link is a named connection definition stored in your schema. It lets a SQL statement refer to a table in another database with the @link_name suffix.
The tutorial's accounts table could live in two regional databases. A transfer between a customer in one region and a customer in the other would then update two tables in two databases.
-- Run once by a user who has CREATE DATABASE LINK privilege.
-- Replace the credentials and the connect string with your own values.
CREATE DATABASE LINK branch_b
CONNECT TO app_user IDENTIFIED BY "app_password"
USING 'branch_b_tns';With the link in place, one transaction can change both databases:
UPDATE accounts SET balance = balance - 100 WHERE id = 1; -- local database
UPDATE accounts@branch_b SET balance = balance + 100 WHERE id = 2; -- remote database
COMMIT;There is no special syntax for "start a distributed transaction". The transaction becomes distributed when a statement in it modifies data through a database link. The two updates must still succeed or fail together, which is the atomicity promise from the ACID chapter. The difficulty is that two separate databases each have their own redo, undo, and commit machinery.
Why a plain COMMIT is not enough
Suppose Oracle simply sent a COMMIT to each database in turn. If the first database committed and the network failed before the second one did, the money would leave one account and never arrive in the other. Neither database could detect the problem alone, because each one saw a valid, complete local transaction.
The protocol that closes this gap is two-phase commit, usually written 2PC. Oracle runs it automatically when you issue COMMIT on a distributed transaction. You do not call it yourself.
The roles in a distributed transaction
Each database in the transaction plays one or more roles. The names matter because the diagnostic views and error messages use them.
| Role | Meaning |
|---|---|
| Client | A node that references data in another database during the transaction. |
| Database server | A node that another node asks for data. |
| Local coordinator | The node that starts the transaction and references other nodes. It coordinates the nodes it references directly. |
| Global coordinator | The node where the transaction originated. It sends the commit or rollback instruction to everyone. |
| Commit point site | The one node that decides, and whose state is the final truth about the outcome. |
Oracle chooses the commit point site using the COMMIT_POINT_STRENGTH initialization parameter. The node with the highest value wins. A node that holds important data, or one that is very reliable, should have a higher strength. The commit point site is the only node that does not enter the "prepared" state described below, which is why it should be the most reliable one.
The two phases
The commit protocol has a prepare phase and a commit phase.
Phase 1: prepare
The global coordinator asks every node except the commit point site to prepare. A node that agrees does three things:
- It writes the transaction's redo to disk, so its changes survive a crash.
- It keeps its locks on the changed rows.
- It records that it is in the prepared state and promises to commit or roll back later, whichever way the coordinator decides.
A node that cannot make that promise answers with an abort, and the whole transaction is rolled back. A node in the prepared state has given up the right to decide on its own. That is the central point of the protocol.
Phase 2: commit
If every node answers that it is prepared, the commit point site commits first. That commit is the moment the whole distributed transaction becomes permanent. The coordinator then tells the other nodes to commit. Each releases its locks, and the coordinator tells the commit point site to forget the transaction, which lets it clean up its records.
Because the commit point site commits before the others hear about it, there is a short window in which the transaction is already committed but some nodes are still prepared. That window is where in-doubt transactions come from.
In-doubt transactions
An in-doubt transaction is a distributed transaction in which a node is prepared but has not heard whether to commit or roll back. This can happen if the network fails, or a node crashes, between the two phases.
While a transaction is in doubt, the prepared node keeps its row locks. Other sessions that try to change those rows wait. Queries still see the old committed data, as usual with read consistency, but they may fail with an error if they try to read a block that the in-doubt transaction changed and the undo needed for a consistent view is no longer available.
In most cases Oracle's recovery process, RECO, resolves the transaction on its own. RECO is a background process that runs when distributed transactions are enabled. It reconnects to the other nodes, finds out the outcome from the commit point site, and finishes the commit or rollback. You do not need to do anything if the network comes back in reasonable time.
Looking at pending transactions
Two data dictionary views show what is waiting. Both need the SELECT_CATALOG_ROLE role, or equivalent privileges.
SELECT local_tran_id, global_tran_id, state, mixed, advice
FROM dba_2pc_pending;
SELECT local_tran_id, in_out, database, interface
FROM dba_2pc_neighbors;DBA_2PC_PENDING lists transactions that are in doubt on this node, with their state (for example prepared, committed, or collecting). DBA_2PC_NEIGHBORS lists the connections between nodes for each transaction, so you can see which remote database is involved.
Manual resolution
If a failure lasts so long that the locks cannot wait, a DBA can force an outcome by hand. The usual first step is to learn the real outcome from the commit point site or from the application owner, and then apply the same outcome locally.
-- Use the LOCAL_TRAN_ID value from DBA_2PC_PENDING.
COMMIT FORCE '12.34.567';
-- or
ROLLBACK FORCE '12.34.567';
-- After the other nodes are consistent, remove the leftover record.
EXECUTE DBMS_TRANSACTION.PURGE_LOST_DB_ENTRY('12.34.567');The FORCE clause only applies to the local node. If you guess wrong and the commit point site actually committed while you rolled back, the data in the two databases now disagrees. Oracle marks such a transaction as mixed outcome (the MIXED column in DBA_2PC_PENDING). Oracle cannot repair it, and someone has to reconcile the data by hand.
Practical consequences
- Locks last longer. Every extra round trip between nodes adds time during which row locks are held. Keep distributed transactions short.
- The first DML statement through a link starts a distributed transaction on the remote side. A SELECT through a link also opens a remote transaction, so you may need to end it with COMMIT or ROLLBACK to release it, even when you only read.
- A remote failure can interrupt your session. An error on the remote database comes back as a normal Oracle error, and statement-level atomicity still applies to the failed statement. Your earlier work in the transaction remains open until you decide what to do.
- Savepoints work inside a distributed transaction, but a rollback to a savepoint only undoes work on the nodes that took part after that savepoint was set.
- DDL through a link still causes implicit commits, which in a distributed transaction triggers the full 2PC protocol.
- Autonomous transactions cannot join the caller's distributed transaction. They run as their own independent transaction.
When to avoid them
Two-phase commit gives you atomicity across databases, but it makes availability depend on every participant being reachable at commit time. If either side is down in the prepare phase, the whole transaction fails. For high-volume or loosely coupled systems, designers often avoid it. Common alternatives are:
- Keep the data that must change together in one database.
- Use a queue or outbox table, so the second database is updated later and the change is retried until it succeeds.
- Accept eventual consistency and add a compensating action that reverses the first step if the second one fails.
Each of these trades the strict guarantee for simpler operations. Choose 2PC when both changes truly must succeed together and both databases are reliably reachable.
Summary
A transaction becomes distributed when it modifies data in more than one Oracle database through database links. COMMIT then runs two-phase commit: all nodes prepare, the commit point site commits, and the others follow. Failures between those steps leave in-doubt transactions, which RECO usually resolves. When it cannot, a DBA inspects DBA_2PC_PENDING and forces the same outcome the commit point site reached.
Exercise
A transfer updates accounts locally and accounts@branch_b remotely, then you issue COMMIT. The network to branch_b fails after branch_b has answered "prepared" but before it receives the commit instruction. What state is the remote transaction in, what happens to its row locks, and what should a DBA check before using COMMIT FORCE or ROLLBACK FORCE?
Show answer
The remote transaction is in doubt (state prepared). It keeps its row locks, so other sessions that try to change those rows wait. Usually RECO resolves it automatically when the network returns. If a DBA must force it, they should first find out what the commit point site did, using DBA_2PC_PENDING and contact with the other site, and apply the same outcome. A wrong forced outcome creates a mixed-outcome transaction.
Exercise
Why does the commit point site not enter the prepared state, and how do you influence which node becomes the commit point site?
Show answer
The commit point site's own commit is the event that makes the whole distributed transaction permanent, so it decides the outcome instead of waiting for someone else's decision. You influence the choice by setting the COMMIT_POINT_STRENGTH initialization parameter. The node with the highest value becomes the commit point site.