Lesson 8 of 8 · Structured Query Language (SQL)

Managing Transactions with TCL

How COMMIT, ROLLBACK, and SAVEPOINT control when changes become permanent, how transactions start and end, and how locks and autocommit affect you.

Beginner 10 sections 5 min read

What TCL does

TCL (Transaction Control Language) decides when changes become permanent. COMMIT saves them, ROLLBACK undoes them, and SAVEPOINT marks a point you can go back to.

A transaction is a group of changes that succeed or fail together, like the two halves of a bank transfer. TCL is how you mark where a transaction ends, and what happens to its changes.

How a transaction starts and ends

In Oracle, you never type "begin". A transaction starts automatically with your first INSERT, UPDATE, DELETE, or MERGE, and every change after that belongs to it until the transaction ends.

A transaction ends when…Its changes are…
You run COMMITSaved for good
You run ROLLBACKUndone
You run any DDL or DCL statement, such as CREATE, ALTER, or GRANTSaved: DDL commits automatically
You exit SQL*Plus normallySaved: SQL*Plus commits on exit by default
The session is killed, the connection drops, or the database crashesUndone

COMMIT: saving changes

COMMIT makes every change in the current transaction permanent. It also makes them visible to other users, and releases the locks the transaction held.

UPDATE sales.orders SET status = 'SHIPPED' WHERE order_id = 105;

1 row updated.

COMMIT;

Commit complete.

Until you commit, nobody else can see your changes. They keep seeing the data as it was:

uncommitted: only session A sees SHIPPED Session A makes the change Session B another user UPDATE 105 COMMIT SELECT 105 SELECT 105 sees PENDING sees SHIPPED time →

This is the "isolation" in ACID: other users never see half-finished work.

ROLLBACK: undoing changes

ROLLBACK undoes every change since the last commit, as if it never happened:

DELETE FROM sales.orders;

6 rows deleted.
-- That was meant to have a WHERE clause!

ROLLBACK;

Rollback complete.

SELECT COUNT(*) FROM sales.orders;

  COUNT(*)
----------
         6

This is why the previous lesson said to check the row count before committing. Until you commit, a mistake costs nothing.

SAVEPOINT: undoing part of a transaction

A savepoint marks a point inside a transaction. ROLLBACK TO SAVEPOINT undoes only the changes made after it, and keeps the earlier ones:

undo back to the savepoint INSERT SAVEPOINT UPDATE ROLLBACK TO COMMIT order 107 before_discount a mistake before_discount order 107 saved (undone)
INSERT INTO sales.orders (order_id, customer_id, amount) VALUES (107, 5, 220);
1 row created.

SAVEPOINT before_discount;
Savepoint created.

UPDATE sales.orders SET amount = amount * 0.9;   -- meant for one order, hit all of them
7 rows updated.

ROLLBACK TO SAVEPOINT before_discount;
Rollback complete.

COMMIT;   -- saves order 107; the discount never happened
Commit complete.

After ROLLBACK TO SAVEPOINT, the transaction is still open. You still need to COMMIT or ROLLBACK to end it.

When a statement fails

If one statement fails partway through, Oracle undoes just that statement, and the rest of the transaction is untouched:

UPDATE sales.orders SET status = 'SHIPPED' WHERE order_id = 106;
1 row updated.

UPDATE sales.orders SET amount = amount - 100;   -- would make some amounts negative
ORA-02290: check constraint (SALES.ORDERS_AMOUNT_CK) violated

The second update changed no rows at all, not even the ones that would have passed the check. The first update is still pending in the transaction, waiting for you to commit or roll it back.

Locks and forgotten transactions

When you change a row, Oracle locks it until your transaction ends. Others can still read the row, seeing the last committed version, but anyone else who tries to change it has to wait.

That makes an uncommitted transaction left open, for example by someone who updated a row and went to lunch, a common cause of "the application is frozen" calls. Every session that touches that row waits behind it. DBAs find the session that's blocking the others with:

SELECT sid, username, blocking_session, event
FROM   v$session
WHERE  blocking_session IS NOT NULL;

       SID USERNAME   BLOCKING_SESSION EVENT
---------- ---------- ---------------- ------------------------------
       142 AMA                      87 enq: TX - row lock contention

Here, Ama's session (142) is waiting for session 87 to commit or roll back.

Autocommit

Some tools commit automatically after every statement. This is called autocommit:

  • SQL*Plus and SQL Developer: off by default. You decide when to commit.
  • Many application drivers, such as Java's JDBC: on by default. Developers turn it off to group statements into real transactions.

With autocommit on, there's nothing to roll back: every statement is permanent the moment it runs. Know your tool's setting before you change production data. In SQL*Plus, SHOW AUTOCOMMIT tells you.

Good habits

  • Commit in logical units. Commit when a piece of business work is complete, such as a whole transfer, not after every row and not hours later.
  • Keep transactions short. The longer a transaction stays open, the longer it holds locks and blocks others.
  • Check before you commit. Verify the row counts, and query the changed rows if in doubt.
  • Never leave a session open with uncommitted changes. Commit or roll back before you walk away.

Key takeaways

  • A transaction starts with your first change and ends with COMMIT, ROLLBACK, any DDL, or the end of the session.
  • COMMIT saves changes, makes them visible to others, and releases locks. ROLLBACK undoes everything since the last commit.
  • SAVEPOINT and ROLLBACK TO SAVEPOINT undo part of a transaction.
  • Other users never see your uncommitted changes, but they wait if they try to change the same rows.
  • DDL and a normal SQL*Plus exit both commit automatically, and some tools autocommit every statement.
  • Keep transactions short, commit in logical units, and never walk away from uncommitted changes.