Sub topic 1
What is a transaction in Oracle?
A transaction is a logical unit of work made of one or more SQL statements. Oracle treats the statements as a group: either all of their changes become permanent, or none of them do. This chapter explains what a transaction is, when it begins and ends, and what you can observe while one is open. Later chapters cover COMMIT, ROLLBACK, savepoints, and the internals in detail.
Prerequisites
This chapter assumes you can:
- Write basic SQL: SELECT, INSERT, UPDATE, and DELETE.
- Connect to an Oracle database with SQL*Plus, SQL Developer, or SQLcl.
- Read a simple PL/SQL block. This is only needed in later chapters.
You also need a schema where you can create a table. The examples use one small table, created below.
Why transactions exist
Many business tasks need more than one statement. A money transfer subtracts from one account and adds to another. If the system fails between the two updates, money disappears. A transaction prevents this. The two updates succeed together or fail together.
A transaction is defined by the business meaning of the work, not by the number of statements. One statement can be a transaction. A hundred statements can also be one transaction.
The example table
All examples in this tutorial start from this table and these two committed rows.
CREATE TABLE accounts (
id NUMBER PRIMARY KEY,
owner VARCHAR2(50) NOT NULL,
balance NUMBER(12,2) NOT NULL CHECK (balance >= 0)
);
INSERT INTO accounts (id, owner, balance) VALUES (1, 'Alice', 1000);
INSERT INTO accounts (id, owner, balance) VALUES (2, 'Bob', 200);
COMMIT;The CHECK constraint matters later. It makes some updates fail, which lets us see what Oracle does with a failed statement inside a transaction.
When a transaction begins
Oracle has no BEGIN TRANSACTION statement. A transaction begins implicitly with the first executable SQL statement that needs one. In practice this means the first DML statement (INSERT, UPDATE, DELETE, MERGE) after a connection starts or after the previous transaction ended. A SELECT ... FOR UPDATE also starts a transaction, because it locks rows.
A plain SELECT does not start a read-write transaction. It reads data and takes no row locks.
The one explicit way to begin a transaction is SET TRANSACTION. It must be the first statement of the transaction, and it is used to choose a special mode such as READ ONLY or SERIALIZABLE. A later chapter covers it.
When a transaction ends
A transaction ends in one of these ways:
| Event | Result |
|---|---|
| `COMMIT` | Changes become permanent and visible to other sessions. |
| `ROLLBACK` (without a savepoint) | All changes since the transaction began are discarded. |
| A DDL statement such as `CREATE TABLE` | Oracle commits the open transaction before and after the statement. |
| Normal disconnect, such as `EXIT` in SQL*Plus | The transaction is committed in most tools. Check your tool's setting. |
| Abnormal termination, such as a crashed session | The transaction is rolled back. |
The next transaction begins with the next statement that needs one. There is no gap: you are always either inside a transaction or waiting for the first statement of the next one.
A first transaction
This transfer is one transaction made of two updates.
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;
-- Or, if something went wrong before COMMIT:
-- ROLLBACK;The first UPDATE starts the transaction. Until COMMIT, the changes are visible only to your own session. If you ran ROLLBACK instead of COMMIT, both balances would return to their earlier values. Other sessions would never have seen the change.
What you can see while a transaction is open
Your session sees its own uncommitted changes. Other sessions do not. Suppose you run only the first UPDATE above and then query the table.
SELECT id, balance FROM accounts ORDER BY id;In your session, Alice shows 900. In a second session, Alice still shows 1000, because that session reads only committed data. Oracle does not make the second session wait. It reconstructs the older version of the row from undo data. Chapter "What happens inside: undo and redo" explains how.
Writers behave differently. If a second session tries to update the row you have changed, it waits until your transaction ends. This is a row lock. Chapter "Row locks and what other sessions see" covers it.
Statements are atomic too
A single SQL statement is itself atomic. If an UPDATE changes 50 rows and fails on the 30th, Oracle undoes the first 29 changes of that statement. The rest of the transaction stays open, and earlier statements keep their changes.
Try it with the example table. This statement tries to take Bob's balance below zero.
UPDATE accounts SET balance = balance - 100 WHERE id = 2;
UPDATE accounts SET balance = balance - 5000 WHERE id = 2;The second statement fails with ORA-02290: check constraint violated. Bob's balance still reflects the first update, which is still uncommitted. You can then choose to COMMIT or ROLLBACK. A later chapter explains this behavior and how savepoints give you finer control.
Checking whether a transaction is open
You can ask the database. The view V$TRANSACTION has one row for each active transaction, and you can link it to your session by its address. You need privileges on the dynamic performance views for this.
SELECT t.xid, t.status, t.start_time
FROM v$transaction t
JOIN v$session s ON s.taddr = t.addr
WHERE s.sid = SYS_CONTEXT('USERENV', 'SID');If the query returns a row, your session has an open transaction. If it returns nothing, you are between transactions. Run the first UPDATE from the transfer, repeat the query, and you will see a row appear. Run ROLLBACK and it disappears.
For a quick check in SQL*Plus, SET AUTOCOMMIT shows whether each statement commits itself. The default is off, which is what you want for transaction practice.
Common mistakes
- Assuming another session sees your changes. Until you commit, only your session sees them. A colleague querying the table sees the old data.
- Leaving a transaction open. An uncommitted
UPDATEholds row locks. Other sessions that touch those rows wait. Forgotten open transactions in a GUI tool are a frequent cause of "the database is hanging" reports. - Expecting ROLLBACK to undo a DDL statement. DDL commits first, so there is nothing left to roll back. The chapter on implicit commits shows an example.
- Trusting tool defaults. SQL Developer and some other clients can have autocommit enabled. Then every DML statement is its own transaction. Check the setting before you test.
- Committing in the middle of a unit of work. If a transfer commits after the debit and fails before the credit, the data is inconsistent. Commit only when the whole business task is done.
Summary
- A transaction is a group of statements that succeed or fail as one unit.
- It begins implicitly with the first statement that needs one, usually DML.
SET TRANSACTIONis the only explicit starter. - It ends with COMMIT, ROLLBACK, DDL, or session termination.
- Your session sees its own uncommitted changes. Other sessions see the last committed data, and writers to the same rows wait.
- Each statement is atomic by itself, and a failed statement does not end the transaction.
Exercise
You run an UPDATE on one row of accounts in session A and do not commit. In session B you run SELECT balance for that row, then try an UPDATE on the same row. What does session B see for the SELECT, and what happens to its UPDATE?
Show answer
The SELECT returns the last committed balance immediately, because Oracle rebuilds the old row version from undo data and readers do not wait. The UPDATE waits on session A's row lock. It proceeds only after session A commits or rolls back.
Exercise
A session runs INSERT INTO accounts (id, owner, balance) VALUES (9, 'Eve', 50); and then CREATE TABLE temp_t (x NUMBER); and then ROLLBACK. Is Eve's row still in the table, and why?
Show answer
Yes. The CREATE TABLE statement is DDL, so Oracle commits the open transaction before running it. The INSERT was committed at that point, and the later ROLLBACK has nothing to undo.