Oracle Database - ROLLBACK (undo of a transaction)

> Database > Oracle Database

1 - Purpose

Use the ROLLBACK statement to undo work done in the current transaction or to manually undo the work done by an in-doubt distributed transaction.

Advertising

3 - Type

3.1 - Implicit

An implicit ROLLBACK occurs when the session (or program) abnormally terminates.

3.2 - Explicit

An explicit commit occurs when the ROLLBACK statement is executed.

4 - Syntax

ROLLBACK [WORK] TO SAVEPOINT;

If the savepoint is unknown, Oracle will display a warning but the transaction can continue.

  • Rollback. Rollback the whole transaction ignoring the savepoints.
ROLLBACK

5 - Statement-Level Rollback

Oracle Database supports statement-level atomicity, which means that a SQL statement is an atomic unit of work and either completely succeeds or completely fails.

A successful statement is different from a committed transaction. A single SQL statement executes successfully if the database parses and runs it without error as an atomic unit, as when all rows are changed in a multirow update.

If a SQL statement causes an error during execution, then it is not successful and so all effects of the statement are rolled back. This operation is a statement-level rollback. This operation has the following characteristics:

  • A SQL statement that does not succeed causes the loss only of work it would have performed itself.

The unsuccessful statement does not cause the loss of any work that preceded it in the current transaction. For example, if the execution of the second UPDATE statement in Figure 10-1 causes an error and is rolled back, then the work performed by the first UPDATE statement is not rolled back. The first UPDATE statement can be committed or rolled back explicitly by the user.

  • The effect of the rollback is as if the statement had never been run.

Any side effects of an atomic statement, for example, triggers invoked upon execution of the statement, are considered part of the atomic statement. Either all work generated as part of the atomic statement succeeds or none does.

An example of an error causing a statement-level rollback is an attempt to insert a duplicate primary key. Single SQL statements involved in a deadlock, which is competition for the same data, can also cause a statement-level rollback. However, errors discovered during SQL statement parsing, such as a syntax error, have not yet been run and so do not cause a statement-level rollback.

Advertising

6 - Example

gerardnico@orcl>CREATE TABLE big_table AS SELECT * FROM all_objects WHERE 1=0;
 
TABLE created.
 
gerardnico@orcl>SELECT COUNT(*) FROM big_table;
 
  COUNT(*)
----------
         0
 
gerardnico@orcl>INSERT INTO big_table SELECT * FROM all_objects
 
66651 rows created.
gerardnico@orcl>SELECT COUNT(*) FROM big_table;
 
  COUNT(*)
----------
     66651
 
gerardnico@orcl>ROLLBACK;
 
ROLLBACK complete.
 
gerardnico@orcl>SELECT COUNT(*) FROM big_table;
 
  COUNT(*)
----------
         0

7 - Reference