This page has instructions for running multi-statement transactions against CockroachDB from various programming languages.
Before you begin
Make sure you have already:
- Set up a local cluster.
- Installed a PostgreSQL client.
- Connected to the database.
- Inserted data that you now want to run queries against.
When running under the default SERIALIZABLE
isolation level, your application should use a retry loop to handle transaction errors that can occur under contention. Client-side retry handling is not necessary under READ COMMITTED
isolation.
Run a transaction
BEGIN;
DELETE FROM customers WHERE id = 1;
DELETE orders WHERE customer = 1;
COMMIT;
For more information about how to use the built-in SQL client, see the cockroach sql
reference docs.
The best way to run a multi-statement transaction from Go code is to use one of the following approaches:
Use the
crdb
transaction wrapper which automatically handles transaction retry errors if they occur, as shown in Build a Go App with CockroachDB.Write your own retry loop wrapper, as shown in Build a Go App with CockroachDB and GORM
The best way to run a multi-statement transaction from Java is to write a wrapper method that automatically handles transaction retry errors.
For complete examples showing how to write and use such wrapper methods, see Build a Java App with CockroachDB.
The best way to run a multi-statement transaction from Python code is to use one of the following approaches:
Use the sqlalchemy-cockroachdb SQLAlchemy dialect, which automatically handles transaction retry errors if they occur, as shown in Build a Python App with CockroachDB and SQLAlchemy.
Write your own retry loop wrapper, as shown in Build a Python App with CockroachDB.
See also
Reference information related to this task:
Other common tasks: