Oracle DataBase Lesson 34 – Transactions & Concurrency Control | Dataplexa

Transactions & Concurrency

To follow along with the examples in this lesson, use the course dataset from Lesson 8.

A transaction is a unit of work that Oracle treats as all-or-nothing. Either every statement in the transaction succeeds and the changes are made permanent, or none of them are. This guarantee is what makes databases reliable for financial, medical, and operational data. Concurrency is what happens when multiple users or sessions work on the same data at the same time — Oracle must ensure they do not interfere with each other in ways that corrupt data or produce incorrect results.

COMMIT and ROLLBACK

A transaction in Oracle begins automatically with the first DML statement — INSERT, UPDATE, or DELETE. It ends when you issue COMMIT to make the changes permanent or ROLLBACK to undo them entirely. Until you commit, your changes are visible only to your own session. Other sessions see the data as it was before your transaction started. If your session disconnects without committing, Oracle automatically rolls back any uncommitted changes.

-- A transaction begins automatically with the first DML statement
UPDATE dataplexa_store.orders
SET    status = 'shipped'
WHERE  order_id = 1001;
UPDATE dataplexa_store.orders
SET    status = 'shipped'
WHERE  order_id = 1002;
-- Both updates succeed -- make them permanent
COMMIT;
-- Another transaction: something goes wrong mid-way
UPDATE dataplexa_store.products
SET    price = price * 1.10
WHERE  category = 'Electronics';
-- Discovered the wrong category was updated -- undo everything since last COMMIT
ROLLBACK;
-- Verify: the electronics prices are unchanged
SELECT product_id, product_name, price
FROM   dataplexa_store.products
WHERE  category = 'Electronics';
-- After COMMIT
Commit complete.

-- After ROLLBACK
Rollback complete.

-- SELECT after rollback -- original prices are intact
PRODUCT_ID | PRODUCT_NAME | PRICE
-----------|--------------------|--------
3 | Laptop Pro 15 | 1299.99
7 | Monitor 27-inch | 319.99
12 | Wireless Keyboard | 89.99
(3 rows selected)
  • DDL statements — CREATE, ALTER, DROP — issue an implicit COMMIT before and after they execute in Oracle, ending any open transaction
  • A ROLLBACK only undoes changes back to the last COMMIT — it cannot undo a committed transaction
  • In SQL Developer, auto-commit is off by default — always COMMIT explicitly when you intend changes to be permanent
  • If a session crashes or disconnects without committing, Oracle automatically rolls back the open transaction during instance recovery

SAVEPOINT

A savepoint marks a point within a transaction that you can roll back to without undoing the entire transaction. This is useful in long transactions where you want to undo part of the work — from the savepoint forward — while keeping earlier changes intact. After rolling back to a savepoint the transaction is still open and you can continue working or commit the remaining changes.

-- Insert a new customer
INSERT INTO dataplexa_store.customers (customer_id, first_name, last_name, email)
VALUES (101, 'Alex', 'Morgan', 'alex.morgan@email.com');
-- Mark a savepoint after the customer insert
SAVEPOINT after_customer;
-- Insert an order for the new customer
INSERT INTO dataplexa_store.orders (order_id, customer_id, order_date, total_amount, status)
VALUES (5001, 101, SYSDATE, 0, 'pending');
-- Something is wrong with the order data -- roll back only to the savepoint
-- The customer insert is preserved; the order insert is undone
ROLLBACK TO SAVEPOINT after_customer;
-- The transaction is still open -- the customer row is still pending commit
-- Fix the order and insert correctly
INSERT INTO dataplexa_store.orders (order_id, customer_id, order_date, total_amount, status)
VALUES (5001, 101, SYSDATE, 249.99, 'pending');
-- Commit both the customer and the corrected order
COMMIT;
-- INSERT customer
1 row created.

-- SAVEPOINT
Savepoint created.

-- INSERT order (bad data)
1 row created.

-- ROLLBACK TO SAVEPOINT after_customer
Rollback complete.

-- INSERT order (corrected)
1 row created.

-- COMMIT
Commit complete.
  • ROLLBACK TO SAVEPOINT name undoes all changes made after the savepoint — the savepoint itself and everything before it remain intact
  • After a ROLLBACK TO SAVEPOINT the transaction is still open — a full COMMIT or ROLLBACK is still required to close it
  • Savepoints are session-local — they are not visible to other sessions and disappear when the transaction ends
  • You can define multiple savepoints in one transaction — each must have a unique name within that transaction

Oracle's Multi-Version Read Consistency

Oracle solves one of the hardest problems in database concurrency — keeping reads accurate without blocking writers — through multi-version read consistency (MVCC). When a query starts, Oracle notes the current System Change Number (SCN). For the duration of the query, Oracle shows only data that was committed before that SCN, using the undo tablespace to reconstruct older versions of rows that have since been changed. Your query always sees a consistent snapshot of the data as it existed at the moment it started — regardless of what other sessions commit while your query is running.

  • Readers do not block writers and writers do not block readers — this is Oracle's fundamental concurrency model
  • A SELECT never waits for another session's uncommitted changes to be resolved — it simply reads the pre-change version from undo
  • If a long-running query exhausts the undo data it needs, Oracle raises ORA-01555: snapshot too old — the undo for the SCN it needs has been overwritten
  • Increasing the undo retention period (UNDO_RETENTION parameter) reduces ORA-01555 occurrences for long-running reports
  • Statement-level read consistency is the default — a single query sees a consistent snapshot; a transaction can optionally use serializable isolation for transaction-level consistency

Locking and Blocking

When a session modifies a row, Oracle places a row-level lock on it. No other session can modify that same row until the first session commits or rolls back. This prevents two sessions from overwriting each other's changes. Reads are never blocked — only writes contend with other writes on the same row. If Session B tries to update a row that Session A has locked, Session B waits. If Session A never commits, Session B waits indefinitely — this is a blocking lock. The SELECT ... FOR UPDATE statement lets a session explicitly lock rows it intends to modify, preventing another session from changing them before the update arrives.

-- SELECT FOR UPDATE: lock the rows you are about to modify
-- Other sessions cannot UPDATE or DELETE these rows until this transaction ends
SELECT order_id, status, total_amount
FROM   dataplexa_store.orders
WHERE  customer_id = 14
FOR UPDATE;
-- Now update the locked rows safely
UPDATE dataplexa_store.orders
SET    status = 'processing'
WHERE  customer_id = 14;
COMMIT;  -- releases the lock
-- NOWAIT: fail immediately instead of waiting if the rows are already locked
SELECT order_id, status
FROM   dataplexa_store.orders
WHERE  order_id = 1001
FOR UPDATE NOWAIT;   -- raises ORA-00054 if another session holds a lock on this row
-- Find sessions that are currently blocking others (requires DBA access)
SELECT blocking_session, sid, serial#, wait_class, seconds_in_wait
FROM   v$session
WHERE  blocking_session IS NOT NULL;
-- SELECT FOR UPDATE (no other session holds a lock)
2 rows selected.

-- UPDATE
2 rows updated.

-- COMMIT -- lock released
Commit complete.

-- FOR UPDATE NOWAIT on a row locked by another session
ORA-00054: resource busy and acquire with NOWAIT specified or timeout expired
  • FOR UPDATE locks only the rows returned by the query — not the entire table
  • FOR UPDATE NOWAIT raises ORA-00054 immediately if any row is locked — useful in applications where waiting is not acceptable
  • FOR UPDATE WAIT n waits up to n seconds before raising an error — a middle ground between waiting indefinitely and failing immediately
  • Deadlocks occur when Session A holds a lock Session B needs and Session B holds a lock Session A needs — Oracle detects these automatically, rolls back one of the statements, and raises ORA-00060
  • V$SESSION is a dynamic performance view — querying blocking_session shows which session is blocking others

Lesson Summary

Concept What It Means
Transaction All-or-nothing unit of work — begins with first DML, ends with COMMIT or ROLLBACK
COMMIT Makes all changes in the current transaction permanent and visible to other sessions
ROLLBACK Undoes all changes since the last COMMIT — transaction is discarded
SAVEPOINT Named marker within a transaction — ROLLBACK TO SAVEPOINT undoes only work done after that point
MVCC Multi-version read consistency — readers see a snapshot of committed data, never block writers
ORA-01555 Snapshot too old — undo data needed by a long query has been overwritten
Row-level lock Oracle locks individual rows during DML — other sessions can modify different rows freely
SELECT FOR UPDATE Explicitly locks rows before modifying them — released on COMMIT or ROLLBACK
ORA-00054 Raised by FOR UPDATE NOWAIT when the target rows are already locked by another session
ORA-00060 Deadlock detected — Oracle rolls back one statement automatically to break the cycle

Practice Questions

Practice 1. When does a transaction begin in Oracle, and what two statements can end it?



Practice 2. You roll back to a savepoint in the middle of a transaction. Is the transaction now closed?



Practice 3. Session A updates a row but has not committed. Session B runs a SELECT on the same row. What does Session B see?



Practice 4. What error does Oracle raise when a DDL statement is executed inside an open transaction?



Practice 5. What is the difference between FOR UPDATE NOWAIT and FOR UPDATE WAIT 5?



Quiz

Quiz 1. You run a DROP TABLE statement while a transaction with two uncommitted UPDATEs is open. What happens to those UPDATEs?






Quiz 2. What causes ORA-01555: snapshot too old?






Quiz 3. Session A holds a row lock that Session B is waiting for. Session A never commits. What happens to Session B?






Quiz 4. Which statement best describes Oracle's multi-version read consistency?






Quiz 5. What does Oracle do when it detects a deadlock?






Next up — Backup & Recovery (RMAN) — How Oracle's Recovery Manager protects your data, how to take full and incremental backups, and how to restore and recover a database.