> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cockroachlabs.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Run Multi-Statement Transactions

This page has instructions for running [multi-statement transactions](https://docs.cockroachlabs.com/docs/v23.2/transactions.md#batched-statements) against CockroachDB from various programming languages.

## Before you begin

Make sure you have already:

* Set up a [local cluster](https://docs.cockroachlabs.com/docs/v23.2/secure-a-cluster.md).
* [Installed a PostgreSQL client](https://docs.cockroachlabs.com/docs/v23.2/install-client-drivers.md).
* [Connected to the database](https://docs.cockroachlabs.com/docs/v23.2/connect-to-the-database.md).
* [Inserted data](https://docs.cockroachlabs.com/docs/v23.2/insert-data.md) that you now want to run queries against.

> [!NOTE]
>
> Your application should [use a retry loop to handle transaction errors](https://docs.cockroachlabs.com/docs/v23.2/query-behavior-troubleshooting.md#transaction-retry-errors) that can occur under [contention](https://docs.cockroachlabs.com/docs/v23.2/performance-best-practices-overview.md#understanding-and-avoiding-transaction-contention).

## Run a transaction

> [tab] SQL
>
> ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> 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`](https://docs.cockroachlabs.com/docs/v23.2/cockroach-sql) reference docs.

> [tab] Go
>
> 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](https://github.com/cockroachdb/cockroach-go/tree/master/crdb) which automatically handles transaction retry errors if they occur, as shown in [Build a Go App with CockroachDB](https://docs.cockroachlabs.com/docs/v23.2/build-a-go-app-with-cockroachdb.md).
> * Write your own retry loop wrapper, as shown in [Build a Go App with CockroachDB and GORM](https://docs.cockroachlabs.com/docs/v23.2/build-a-go-app-with-cockroachdb-gorm.md)

> [tab] Java
>
> 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](https://docs.cockroachlabs.com/docs/v23.2/build-a-java-app-with-cockroachdb.md).

> [tab] Python
>
> The best way to run a multi-statement transaction from Python code is to use one of the following approaches:
>
> * Use the [sqlalchemy-cockroachdb](https://github.com/cockroachdb/sqlalchemy-cockroachdb) SQLAlchemy dialect, which automatically handles transaction retry errors if they occur, as shown in [Build a Python App with CockroachDB and SQLAlchemy](https://docs.cockroachlabs.com/docs/v23.2/build-a-python-app-with-cockroachdb-sqlalchemy.md).
> * Write your own retry loop wrapper, as shown in [Build a Python App with CockroachDB](https://docs.cockroachlabs.com/docs/v23.2/build-a-python-app-with-cockroachdb.md).

## See also

Reference information related to this task:

* [Transactions](https://docs.cockroachlabs.com/docs/v23.2/transactions.md)
* [Client-side transaction retries](https://docs.cockroachlabs.com/docs/v23.2/transaction-retry-error-reference.md#client-side-retry-handling)
* [Batched statements](https://docs.cockroachlabs.com/docs/v23.2/transactions.md#batched-statements)
* [Transaction Contention](https://docs.cockroachlabs.com/docs/v23.2/performance-best-practices-overview.md#transaction-contention)
* [`BEGIN`](https://docs.cockroachlabs.com/docs/v23.2/begin-transaction)
* [`COMMIT`](https://docs.cockroachlabs.com/docs/v23.2/commit-transaction)

Other common tasks:

* [Connect to the Database](https://docs.cockroachlabs.com/docs/v23.2/connect-to-the-database.md)
* [Insert Data](https://docs.cockroachlabs.com/docs/v23.2/insert-data.md)
* [Query Data](https://docs.cockroachlabs.com/docs/v23.2/query-data.md)
* [Update Data](https://docs.cockroachlabs.com/docs/v23.2/update-data.md)
* [Delete Data](https://docs.cockroachlabs.com/docs/v23.2/delete-data.md)
* [Optimize Statement Performance](https://docs.cockroachlabs.com/docs/v23.2/make-queries-fast.md)
* [Troubleshoot SQL Statements](https://docs.cockroachlabs.com/docs/v23.2/query-behavior-troubleshooting.md)
* [Example Apps](https://docs.cockroachlabs.com/docs/v23.2/example-apps)
