1. After a Successful Transaction
When you've finished a series of related DML operations (INSERT, UPDATE, DELETE) and confirmed everything is correct:
-- Example
UPDATE accounts SET balance = balance - 500 WHERE account_id = 1;
UPDATE accounts SET balance = balance + 500 WHERE account_id = 2;
COMMIT;
This ensures both operations are saved together — ensuring transactional integrity.
2. After a Batch Job or Data Load
When you're inserting or updating large volumes of data (e.g., from a script or batch job), you may:
- Commit periodically (e.g., every 1,000 rows), or
- Commit at the end of the load, once all data has been verified.
3. After Validation
Only issue a COMMIT after:
- You’ve validated data manually or programmatically.
- Business rules and constraints have been met.
4. To Release Locks
If your transaction is holding locks on rows/tables, other users may be blocked. COMMIT releases those locks:
- Especially important in multi-user environments.
- Prevents long-running uncommitted transactions from causing contention.
5. At Logical Boundaries
In long-running processes or scripts, commit at logical checkpoints:
FORALL i IN 1..10000
INSERT INTO sales VALUES (...);
-- Commit after every 1000 records
IF MOD(i, 1000) = 0 THEN
COMMIT;
END IF;
When NOT to Use COMMIT
🔸 After Every Statement (Unless Required)
- Avoid committing after every DML unless it's a completely independent change.
- Doing so breaks transaction consistency and makes rollback impossible.
🔸 Before Validating Data
- Don’t commit if you haven't verified the correctness of your changes.
🔸 Inside Triggers
- Oracle does not allow COMMIT inside triggers, as it would break transaction integrity.
Summary: Use COMMIT when...
Scenario |
Use COMMIT? |
All DML is successful and verified |
✅ Yes |
Before ROLLBACK possibility is needed |
❌ No |
After critical error or uncertainty |
❌ No |
To release locks after successful operation |
✅ Yes |
Inside a trigger |
❌ Not allowed |
No comments:
Post a Comment