Sub topic 5
Implicit commits: DDL, normal exit, and abnormal termination
You have used COMMIT and ROLLBACK as explicit commands. Oracle also ends transactions on its own in some situations. Some of these commit your work, and others discard it. This chapter assumes you know how a transaction begins and what explicit COMMIT and ROLLBACK do. It also assumes the accounts table from the earlier chapters, with rows 1 (Alice) and 2 (Bob) committed. Knowing these rules lets you predict what survives when a session does something you did not plan for.
The events that end a transaction without your command
A transaction ends in one of these ways:
- You issue
COMMITorROLLBACK. - You run a DDL statement, which commits.
- The session exits normally, and the client tool usually commits.
- The session terminates abnormally, and Oracle rolls back.
The last three are the subject of this chapter. The diagram shows how they relate.
Implicit commits caused by DDL
DDL (data definition language) statements create, change, or remove database objects. Oracle issues a COMMIT before a DDL statement runs and another one after it succeeds. DDL cannot be part of a larger transaction that you can later undo.
Statements that behave this way include:
CREATE,ALTER,DROP,TRUNCATE, andRENAMEGRANTandREVOKECOMMENTandAUDIT
The example below shows the effect. It uses the accounts table from the first chapter.
INSERT INTO accounts (id, owner, balance) VALUES (4, 'Dan', 10);
CREATE TABLE scratch_table (x NUMBER); -- DDL commits the open transaction first
ROLLBACK; -- nothing left to undo
SELECT owner FROM accounts WHERE id = 4; -- still returns 'Dan'The INSERT of Dan has nothing to do with scratch_table. The commit before the DDL still covers it, because Oracle commits the whole session transaction, not only the objects the DDL touches.
The commit happens even if the DDL fails
The commit before the DDL statement happens first, so a DDL statement that fails at run time still leaves your earlier work committed. For example:
INSERT INTO accounts (id, owner, balance) VALUES (5, 'Eve', 20);
CREATE TABLE accounts (id NUMBER); -- fails: ORA-00955 name is already used
ROLLBACK; -- Eve's row is already committedThe error does not undo the earlier implicit commit. If you need DDL in a script, put a deliberate COMMIT or ROLLBACK before it so the state of the transaction is a decision you made, not a side effect.
DDL inside PL/SQL
PL/SQL cannot run DDL directly, but EXECUTE IMMEDIATE can. It commits just like DDL typed at the prompt.
BEGIN
INSERT INTO accounts (id, owner, balance) VALUES (6, 'Fay', 30);
EXECUTE IMMEDIATE 'CREATE TABLE tmp_fay (x NUMBER)'; -- implicit commit here
ROLLBACK; -- cannot undo Fay's row
END;
/
SELECT owner FROM accounts WHERE id = 6; -- returns 'Fay'
DROP TABLE tmp_fay;
DROP TABLE scratch_table;This is a common source of bugs. A procedure that ran DML and then called a helper issuing DDL, such as a TRUNCATE of a staging table, has committed the DML. The caller's later ROLLBACK does nothing for that work.
Statements that do not commit
Not every statement that sounds administrative commits. SELECT, ALTER SESSION, SET ROLE, and SET TRANSACTION do not end your transaction. SET TRANSACTION must be the first statement of a transaction, so it raises an error if you run it after DML without ending that transaction first.
A DDL statement run inside an autonomous transaction commits only that autonomous transaction. It does not commit the caller. Autonomous transactions are covered in a later chapter.
Checking whether a transaction is open
When you are unsure what a statement did, ask Oracle. DBMS_TRANSACTION.LOCAL_TRANSACTION_ID returns the identifier of the current transaction, or NULL if none is open.
SELECT DBMS_TRANSACTION.LOCAL_TRANSACTION_ID AS txn FROM dual; -- NULL
UPDATE accounts SET balance = balance + 1 WHERE id = 2;
SELECT DBMS_TRANSACTION.LOCAL_TRANSACTION_ID AS txn FROM dual; -- for example 8.17.1204
CREATE TABLE probe (x NUMBER); -- implicit commit
SELECT DBMS_TRANSACTION.LOCAL_TRANSACTION_ID AS txn FROM dual; -- NULL again
DROP TABLE probe;The identifier has three parts: undo segment number, slot, and sequence. You do not need to decode it. A non-null value means a transaction is open. NULL after a statement means that statement ended the transaction.
Normal exit and disconnect
When a session ends cleanly, Oracle itself does not decide whether to commit. The client tool or driver does. This is where behavior differs between tools.
| Situation | Typical result |
|---|---|
| SQL*Plus `EXIT` or `QUIT` | Commits, because `EXITCOMMIT` defaults to `ON` |
| SQL*Plus `EXIT` with `SET EXITCOMMIT OFF` | Rolls back pending work |
| SQL*Plus `DISCONNECT` or `CONNECT` to another user | Commits the current transaction first |
| SQL*Plus `SET AUTOCOMMIT ON` | Commits after each successful DML statement or PL/SQL block |
| GUI tools such as SQL Developer | Usually ask or follow a preference setting |
| Application drivers | Depends on the driver and its autocommit setting |
Autocommit has a serious consequence. With it on, every statement becomes its own transaction, so you cannot group a debit and a credit together and you cannot roll back after a mistake. The money transfer from the earlier chapter would be unsafe with autocommit on.
For application code, do not rely on close behavior. Some drivers commit a pending transaction when the connection closes, and others roll it back. Check your driver's documentation, and always end a transaction explicitly before closing.
Abnormal termination
An abnormal termination is any end of the session that is not a clean exit. Examples are a client process crash, a closed terminal window, a lost network connection, an administrator running ALTER SYSTEM KILL SESSION, or a failure of the whole instance.
In all of these cases, uncommitted work is rolled back. Oracle uses the undo data it already stored for the transaction. Two mechanisms do the cleanup:
- For a single dead session, a background process called PMON detects that the session is gone, rolls back its transaction, and releases its locks.
- After an instance crash, instance recovery rolls forward using the redo log and then rolls back every transaction that had no commit record.
Two practical effects follow:
- Locks linger briefly. Until cleanup finishes, other sessions that need the same rows wait. A large transaction can take a while to roll back, because the rollback must reverse every change.
- A crash during
COMMITis all or nothing. A transaction counts as committed once its commit record is safely in the redo log. If the crash happens before that, the whole transaction is rolled back. After that, it is recovered. You never see half of a transaction.
A Ctrl+C in SQL*Plus is different. It usually cancels the running statement and leaves the session and its earlier work in place. The cancelled statement's own changes are rolled back, but earlier statements in the transaction stay pending. Statement-level rollback is the subject of a later chapter.
Common mistakes
- Running DDL in the middle of a unit of work, and then expecting
ROLLBACKto undo the earlier DML. - Assuming a failed DDL statement left the transaction untouched.
- Leaving
AUTOCOMMITon from an earlier session and losing the ability to roll back. - Testing a script that ends in
EXIT, then finding the "test" changes were committed. - Using
TRUNCATEwhereDELETEis needed.DELETEcan be rolled back andTRUNCATEcannot, because it is DDL.
Exercise
A session runs UPDATE accounts SET balance = balance - 50 WHERE id = 1;, then ALTER TABLE accounts ADD (note VARCHAR2(20));, then ROLLBACK;. Is Alice's balance reduced? Would the answer change if the ALTER TABLE failed with an error?
Show answer
Yes, Alice's balance is reduced by 50. The ALTER TABLE is DDL, so Oracle commits the update before running it, and the later ROLLBACK has nothing to undo. If the ALTER TABLE failed at run time, the answer would stay the same, because the commit before the DDL statement had already happened.
Exercise
A user inserts a row, then their laptop loses its network connection before they issue COMMIT. When the database detects the dead session, what happens to the row, and what do other sessions that wanted to update that row experience in the meantime?
Show answer
The row is rolled back, because the session ended abnormally and the transaction had no commit. Until PMON finishes the cleanup and releases the row locks, other sessions trying to change that row wait.