Sub topic 14
Designing Transaction Boundaries and Avoiding Pitfalls
Assume you know basic SELECT, INSERT, UPDATE, and DELETE, can connect to Oracle, and understand basic transaction commands. This chapter focuses on choosing where a transaction should begin and end.
Match the boundary to the business unit
A transaction should represent one complete business operation. All changes needed for that operation should succeed together, or none should remain.
For example, transferring money requires two updates. Committing after the debit but before the credit could remove money from one account without adding it to the other. Put both updates in one transaction and commit only after both succeed.
UPDATE accounts
SET balance = balance - 100
WHERE id = 1;
UPDATE accounts
SET balance = balance + 100
WHERE id = 2;
COMMIT;The application, rather than an unrelated helper procedure, should usually own this boundary. The application knows when the complete unit of work has succeeded.
| Boundary choice | Typical result |
|---|---|
| One transaction for one transfer | Debit and credit remain consistent |
| Commit after every row in a batch | Earlier rows stay committed when a later row fails |
| One transaction for an entire large import | Long locks and heavy undo usage |
Avoid premature and missing commits
A COMMIT makes changes permanent and releases locks. A procedure that commits unexpectedly can break a larger operation controlled by its caller. For example, a procedure that updates an order and commits before inventory processing finishes prevents the caller from rolling back the order.
At the other extreme, leaving a transaction open for a long time holds row locks and consumes undo resources. Other sessions may wait when they try to update the same rows. Commit after the business operation finishes, not after every individual SQL statement.
Do not use COMMIT as an error-handling shortcut. Handle the error first, then decide whether the complete operation should be committed or rolled back.
Use savepoints for local recovery
A savepoint is useful inside one transaction when a recoverable part can be undone without losing earlier work. It does not make earlier changes permanent.
SAVEPOINT before_discount;
UPDATE order_items
SET price = price * 0.90
WHERE order_id = 101;
-- If the discount is invalid:
ROLLBACK TO SAVEPOINT before_discount;
-- Continue with other valid work.
COMMIT;Use savepoints for controlled batch processing or optional steps. Do not use them to replace a clear transaction boundary. A rollback to a savepoint leaves the transaction open, and later savepoints are erased.
Common pitfalls
- DDL, such as
CREATE TABLEorALTER TABLE, causes implicit commits before and after the statement. Keep schema changes separate from business transactions. - A failed SQL statement is normally atomic. Oracle undoes that statement's effects, but it does not automatically roll back all earlier statements in the transaction.
- A client disconnect or abnormal session termination can roll back uncommitted work. Do not depend on this as normal control flow.
- Autonomous logging transactions commit independently. They can preserve an error record, but they must not be used to commit part of the caller's business operation.
- Retrying a transaction requires care. The operation should be idempotent, meaning repeating it does not apply the business effect twice.
A practical decision process
Ask these questions before choosing the boundary:
- What business result must be all-or-nothing?
- Which statements create that result?
- Who owns the final
COMMITorROLLBACK? - Could the transaction hold locks or undo data for too long?
- Can a failed step use a savepoint, or should the whole operation fail?
- Can the operation be safely retried after a timeout?
Exercise
An order procedure updates the order, commits, and then fails while reserving inventory. What design change allows the caller to keep the operation atomic?
Show answer
Remove the procedure's internal COMMIT and let the caller commit only after both the order update and inventory reservation succeed. Roll back the whole transaction if either step fails.