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
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, your application conditionally executes rollbacks, but you can see their behavior by using ROLLBACK
instead of COMMIT
directly through SQL.
> SELECT * FROM accounts;
+----------+---------+
| name | balance |
+----------+---------+
| Marciela | 1000 |
+----------+---------+
> BEGIN;
> UPDATE accounts SET balance = 2500 WHERE name = 'Marciela';
> ROLLBACK;
> SELECT * FROM accounts;
+----------+---------+
| name | balance |
+----------+---------+
| Marciela | 1000 |
+----------+---------+
Retry a Transaction
To use client-side transaction retries, your application must execute ROLLBACK TO SAVEPOINT cockroach_restart
after detecting a 40001
/ retry transaction
error.
> ROLLBACK TO SAVEPOINT cockroach_restart;
For examples of retrying transactions in your application, check out the transaction code samples in our Build an App with CockroachDB tutorials.