The ROLLBACK statement aborts the current transaction, discarding all updates made by statements included in the transaction.

When using client-side transaction retries, use ROLLBACK TO SAVEPOINT cockroach_restart to handle a transaction that needs to be retried (identified via the 40001 error code or retry transaction string in the error message), and then re-execute the statements you want the transaction to contain.

Synopsis

ROLLBACK TO SAVEPOINT cockroach_restart

Required privileges

No privileges are required to rollback a transaction. However, privileges are required for each statement within a transaction.

Parameters

Parameter Description
TO SAVEPOINT cockroach_restart If using client-side transaction retries, retry the transaction. You should execute this statement when a transaction returns a 40001 / retry transaction error.

Example

Rollback a transaction

Typically, an application conditionally executes rollbacks, but we can see their behavior by using ROLLBACK instead of COMMIT directly through SQL:

copy
icon/buttons/copy
> SELECT * FROM accounts;
+----------+---------+
|   name   | balance |
+----------+---------+
| Marciela |    1000 |
+----------+---------+
copy
icon/buttons/copy
> BEGIN;
copy
icon/buttons/copy
> UPDATE accounts SET balance = 2500 WHERE name = 'Marciela';
copy
icon/buttons/copy
> ROLLBACK;
copy
icon/buttons/copy
> SELECT * FROM accounts;
+----------+---------+
|   name   | balance |
+----------+---------+
| Marciela |    1000 |
+----------+---------+

Retry a transaction

To use client-side transaction retries, an application must execute ROLLBACK TO SAVEPOINT cockroach_restart after detecting a 40001 / retry transaction error:

copy
icon/buttons/copy
> ROLLBACK TO SAVEPOINT cockroach_restart;

For examples of retrying transactions in an application, check out the transaction code samples in our Build an App with CockroachDB tutorials.

See also



Yes No