The ROLLBACK statement can be used to either:
This statement can be embedded in an application program or issued interactively. It is an executable statement that can be dynamically prepared.
ROLLBACK is not allowed in a trigger if the trigger program and the triggering program run under the same commitment definition. ROLLBACK is not allowed in a procedure if the procedure is called on a connection to a remote application server or if the procedure is defined as ATOMIC. COMMIT is not allowed in a function.
None required.
.-WORK-.
>>-ROLLBACK--+------+--+-----------------------------------+---><
+-HOLD------------------------------+
'-TO--SAVEPOINT--+----------------+-'
'-savepoint-name-'
When ROLLBACK is used without the TO SAVEPOINT clause, the unit of work in which it is executed is ended. All changes made by SQL schema statements and SQL data change statements during the unit of work are backed out. For more information see Statements.
The generation of identity values is not under transaction control. Values generated and consumed by inserting rows into a table that has an identity column are independent of executing the ROLLBACK statement. Also, executing the ROLLBACK statement does not affect the IDENTITY_VAL_LOCAL function.
Special registers are not under transaction control. Executing a ROLLBACK statement does not affect special registers.
Sequences are not under transaction control. Executing a ROLLBACK statement does not affect the current value generated and consumed by executing a NEXT VALUE expression.
The impact of ROLLBACK or ROLLBACK TO SAVEPOINT on the contents of a declared global temporary table is determined by the setting of the ON ROLLBACK clause of the DECLARE GLOBAL TEMPORARY TABLE statement.
At the end of a ROLLBACK HOLD, the cursor position is the same as it was at the start of the unit of work, unless
After a successful ROLLBACK TO SAVEPOINT, the savepoint continues to exist.
All database changes (including changes made to declared temporary tables that were declared with the ON ROLLBACK PRESERVE ROWS clause) that were made after the savepoint was set are backed out. All locks and LOB locators are retained.
The impact on cursors resulting from a ROLLBACK TO SAVEPOINT depends on the statements within the savepoint:
Any savepoints that are set after the one to which rollback is performed are released. The savepoint to which rollback is performed is not released.
Recommended coding practices: Code an explicit COMMIT or ROLLBACK statement at the end of an application process. Either an implicit commit or rollback operation will be performed at the end of an application process depending on the application environment. Thus, a portable application should explicitly execute a COMMIT or ROLLBACK before execution ends in those environments where explicit COMMIT or ROLLBACK is permitted.
Other effects of rollback: Rollback without the TO SAVEPOINT clause and HOLD clause causes the following to occur:
ROLLBACK has no effect on the state of connections.
Implicit ROLLBACK: The ending of the default activation group causes an implicit rollback. Thus, an explicit COMMIT or ROLLBACK statement should be issued before the end of the default activation group.
A ROLLBACK is automatically performed when:
If the unit of work is in the prepared state because a COMMIT was in progress when the failure occurred, a rollback is not performed. Instead, resynchronization of all the connections involved in the unit of work will occur. For more information, see the Commitment control topic.
If the unit of work is in the prepared state because a COMMIT was in progress when the failure occurred, a rollback is not performed. Instead, resynchronization of all the connections involved in the unit of work will occur. For more information, see the Commitment control topic.
Row lock limit: A unit of work may include the processing of up to and including 4 million rows, including rows retrieved during a SELECT INTO or FETCH statement80, and rows inserted, deleted, or updated as part of INSERT, DELETE, and UPDATE operations.81
Unaffected statements: The commit and rollback operations do not affect the DROP SCHEMA statement, and this statement is not, therefore, allowed if the current isolation level is anything other than No Commit (NC).
ROLLBACK restrictions: A ROLLBACK statement is not allowed if commitment control is not active for the activation group. For information on determining which commitment definition is used, see the commitment definition discussion in the COMMIT statement.
ROLLBACK has no effect on the state of connections.
If, within a unit of work, a CLOSE is followed by a ROLLBACK, all changes made within the unit of work are backed out. The CLOSE itself is not backed out and the file is not reopened.
Example 1: See the Example under COMMIT for examples using the ROLLBACK statement.
Example 2: After a unit of recovery started, assume that three savepoints A, B, and C were set and that C was released:
SAVEPOINT A ON ROLLBACK RETAIN CURSORS; ... SAVEPOINT B ON ROLLBACK RETAIN CURSORS; .... SAVEPOINT C ON ROLLBACK RETAIN CURSORS; ... RELEASE SAVEPOINT C
Roll back all database changes only to savepoint A:
ROLLBACK WORK TO SAVEPOINT A
If a savepoint name was not specified (that is, ROLLBACK WORK TO SAVEPOINT), the rollback would be to the last active savepoint that was set, which is B.