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 Postgres client.
- Connected to the database.
- Inserted data that you now want to run queries against.
Your application should use a retry loop to handle transaction errors that can occur under contention.
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:
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.
Reference information related to this task:
- Transaction retries
- Batched statements
- Understanding and Avoiding Transaction Contention
Other common tasks:
- Connect to the Database
- Insert Data
- Query Data
- Update Data
- Delete Data
- Optimize Statement Performance
- Error Handling and Troubleshooting
- Hello World Example apps