ROLLBACK statement aborts the current transaction and all of its nested transactions, discarding all transactional updates made by statements included in the transaction.
There are two ways to use ROLLBACK:
- The
ROLLBACKstatement rolls back the entire transaction - The
ROLLBACK TO SAVEPOINTstatement rolls back and restarts the nested transaction started at the correspondingSAVEPOINTstatement
Required privileges
No privileges are required to rollback a transaction. However, privileges are required for each statement within a transaction.Syntax
Parameters
If using advanced client-side transaction retries, retry the transaction. You should execute this statement when a transaction returns a
40001 / retry transaction error.If using nested transactions, roll back and restart the nested transaction started at the corresponding
SAVEPOINT statement.Examples
Rollback a transaction
Typically, an application conditionally executes rollbacks, but we can see their behavior by usingROLLBACK instead of COMMIT directly:
Rollback a nested transaction
TheROLLBACK TO SAVEPOINT statement rolls back and restarts the nested transaction started at the corresponding SAVEPOINT statement.
For example, the transaction below will insert the values (1,1) and (3,3) into the table, but not (2,2):
Retry a transaction
When using advanced client-side transaction retries, useROLLBACK TO SAVEPOINT to handle a transaction that needs to be retried (identified via the 40001 error code or restart transaction string in the error message), and then re-execute the statements you want the transaction to contain.
See also
- SAVEPOINT
- BEGIN
- COMMIT
- RELEASE SAVEPOINT
- SHOW SAVEPOINT STATUS
- Transactions