Sub topic 1
What Is a Transaction in Oracle?
A transaction is a logical unit of work in Oracle Database. It groups one or more SQL statements so they are treated as one change to the database. A transaction either becomes permanent with COMMIT or is discarded with ROLLBACK.
For example, transferring money requires two changes:
- Subtract money from the sender.
- Add the same money to the recipient.
Those changes belong in one transaction. If only the first update succeeds, the data is incorrect. A transaction lets you save both updates together or undo both.
Prerequisites
This chapter assumes that you can use SELECT, INSERT, UPDATE, and DELETE. You should also know how to connect to Oracle with SQL*Plus, SQL Developer, or SQLcl.
When a Transaction Begins
Oracle starts a transaction when a session executes its first executable SQL statement after connecting or after the previous transaction ends. In everyday work, this is usually the first INSERT, UPDATE, or DELETE.
The transaction continues while the session runs more statements. It ends when one of these events occurs:
COMMITmakes the changes permanent.ROLLBACKdiscards the changes.- A DDL statement, such as
CREATE TABLEorALTER TABLE, causes an implicit commit. - The session disconnects normally, which commits the transaction.
- The session ends abnormally, which rolls back the uncommitted transaction.
A transaction belongs to a database session. Two sessions can execute statements against the same database, but each session has its own current transaction.
CREATE TABLE transaction_demo (
id NUMBER PRIMARY KEY,
item VARCHAR2(40) NOT NULL,
quantity NUMBER NOT NULL
);
INSERT INTO transaction_demo (id, item, quantity)
VALUES (1, 'Notebook', 10);
UPDATE transaction_demo
SET quantity = quantity + 5
WHERE id = 1;
ROLLBACK;
SELECT quantity
FROM transaction_demo
WHERE id = 1;The ROLLBACK discards both the INSERT and the UPDATE. The final query returns no row because the inserted row was never committed.
The table creation is different. CREATE TABLE is DDL, or data definition language. Oracle commits the transaction before and after most DDL statements. For that reason, create or alter database objects outside a transaction that contains business changes.
A Transaction as a Business Unit
A useful transaction boundary matches one complete business operation. For an order, the boundary might include creating the order, adding its lines, and reducing inventory. The application should commit after all required steps succeed.
The following model shows the normal lifecycle:
Oracle transactions provide the foundation for ACID behavior:
| Property | Meaning |
|---|---|
| Atomicity | The transaction's changes succeed together or are undone together. |
| Consistency | Committed changes must satisfy database rules. |
| Isolation | Other sessions do not see another session's uncommitted changes. |
| Durability | A committed change survives normal database recovery. |
Atomicity does not mean that every failed statement ends the whole transaction. Oracle normally rolls back the failed statement, while earlier successful statements remain pending. You can then correct the problem, roll back the entire transaction, or use a savepoint in more detailed transaction logic.
Exercise
An application inserts an order, updates inventory, and then discovers that payment failed. Which transaction command should it use if none of the changes should remain?
Show answer
It should use ROLLBACK before the transaction is committed.