Sub topic 3
Making Changes Permanent with COMMIT
Prerequisites and the role of COMMIT
This chapter assumes you know SELECT, INSERT, UPDATE, and DELETE, and can connect to Oracle using SQL*Plus, SQL Developer, or SQLcl. A transaction is the group of database changes made since the previous transaction ended. Oracle ends the transaction when you issue COMMIT or ROLLBACK.
COMMIT makes every change in the current transaction permanent. It also ends that transaction. After the commit, another session can see the changes, and Oracle releases the row locks held by your session.
Consider a transfer between two accounts:
UPDATE accounts
SET balance = balance - 100
WHERE id = 1;
UPDATE accounts
SET balance = balance + 100
WHERE id = 2;
COMMIT;The two UPDATE statements are one business operation. Until COMMIT runs, the changes belong only to the current transaction. If the session disconnects or issues ROLLBACK first, the updates are discarded. After COMMIT, a later ROLLBACK cannot undo them.
What COMMIT does
COMMIT performs several related actions:
- Makes the transaction's changes visible to other sessions.
- Records the transaction as complete and durable according to Oracle's commit processing.
- Releases row locks held by the transaction.
- Ends the current transaction.
- Removes any savepoints created in that transaction.
A commit applies to all uncommitted changes in the current session. You cannot commit only one statement from the middle of a transaction. To separate work, commit one unit of work before starting the next one.
| Before COMMIT | After COMMIT |
|---|---|
| Changes are uncommitted in the current session | Changes are permanent |
| Other sessions normally see the previous committed values | Other sessions can read the new values |
| Row locks remain held | Row locks are released |
| Savepoints can be used | The old savepoints no longer exist |
A transaction boundary
A transaction boundary is the point where one unit of database work ends and another begins. A useful boundary usually matches one complete business action, such as placing an order or transferring money.
Committing after every row can leave a partially completed operation if a later row fails. Committing only after an entire batch can hold locks for too long. Choose a boundary that keeps related changes together while limiting the transaction's duration.
The following diagram shows the normal path for a transaction:
Checking your work before committing
Before committing, verify that the intended rows were changed. A SELECT in the same session can see your uncommitted changes:
UPDATE accounts
SET balance = balance - 100
WHERE id = 1;
SELECT id, owner, balance
FROM accounts
WHERE id = 1;
COMMIT;A different session may still see the previously committed balance until the commit occurs. Client tools can also have an autocommit setting. When autocommit is enabled, the tool may issue COMMIT after each successful statement, so check that setting before testing transaction behavior.
Exercise
After two related UPDATE statements succeed, when should you issue COMMIT?
Show answer
Issue COMMIT after both statements succeed and after any needed verification, so both changes become permanent as one business operation.