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

# Run Multi-Statement Transactions

export const InternalLink = ({version, path = "", children, ...props}) => {
  let detectedVersion = version || "stable";
  if (typeof window !== 'undefined' && !version) {
    const match = window.location.pathname.match(/\/docs\/([^/]+)/);
    if (match) {
      detectedVersion = match[1];
    }
  }
  const normalizedPath = path.startsWith("/") ? path.slice(1) : path;
  return <a href={`/docs/${detectedVersion}/${normalizedPath}`} {...props}>
      {children}
    </a>;
};

This page has instructions for running <InternalLink path="transactions#batched-statements">multi-statement transactions</InternalLink> against CockroachDB from various programming languages.

## Before you begin

Make sure you have already:

* Set up a <InternalLink path="secure-a-cluster">local cluster</InternalLink>.
* <InternalLink path="install-client-drivers">Installed a PostgreSQL client</InternalLink>.
* <InternalLink path="connect-to-the-database">Connected to the database</InternalLink>.
* <InternalLink path="insert-data">Inserted data</InternalLink> that you now want to run queries against.

<Note>
  When running under the default <InternalLink path="demo-serializable">`SERIALIZABLE`</InternalLink> isolation level, your application should <InternalLink path="query-behavior-troubleshooting#transaction-retry-errors">use a retry loop to handle transaction retry errors</InternalLink> that can occur under <InternalLink path="performance-best-practices-overview#understanding-and-avoiding-transaction-contention">contention</InternalLink>.
</Note>

## Run a transaction

<Tabs>
  <Tab title="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 <InternalLink path="cockroach-sql">`cockroach sql`</InternalLink> reference docs.
  </Tab>

  <Tab title="Go">
    The best way to run a multi-statement transaction from Go code is to use one of the following approaches:

    * Use the <a href="https://github.com/cockroachdb/cockroach-go/tree/master/crdb">`crdb` transaction wrapper</a> which automatically handles transaction retry errors if they occur, as shown in <InternalLink path="build-a-go-app-with-cockroachdb">Build a Go App with CockroachDB</InternalLink>.
    * Write your own retry loop wrapper, as shown in <InternalLink path="build-a-go-app-with-cockroachdb-gorm">Build a Go App with CockroachDB and GORM</InternalLink>
  </Tab>

  <Tab title="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 <InternalLink path="build-a-java-app-with-cockroachdb">Build a Java App with CockroachDB</InternalLink>.
  </Tab>

  <Tab title="Python">
    The best way to run a multi-statement transaction from Python code is to use one of the following approaches:

    * Use the <a href="https://github.com/cockroachdb/sqlalchemy-cockroachdb">sqlalchemy-cockroachdb</a> SQLAlchemy dialect, which automatically handles transaction retry errors if they occur, as shown in <InternalLink path="build-a-python-app-with-cockroachdb-sqlalchemy">Build a Python App with CockroachDB and SQLAlchemy</InternalLink>.
    * Write your own retry loop wrapper, as shown in <InternalLink path="build-a-python-app-with-cockroachdb">Build a Python App with CockroachDB</InternalLink>.
  </Tab>
</Tabs>

## See also

Reference information related to this task:

* <InternalLink path="transactions">Transactions</InternalLink>
* <InternalLink path="transaction-retry-error-reference#client-side-retry-handling">Client-side transaction retries</InternalLink>
* <InternalLink path="transactions#batched-statements">Batched statements</InternalLink>
* <InternalLink path="performance-best-practices-overview#transaction-contention">Transaction Contention</InternalLink>
* <InternalLink path="begin-transaction">`BEGIN`</InternalLink>
* <InternalLink path="commit-transaction">`COMMIT`</InternalLink>

Other common tasks:

* <InternalLink path="connect-to-the-database">Connect to the Database</InternalLink>
* <InternalLink path="insert-data">Insert Data</InternalLink>
* <InternalLink path="query-data">Query Data</InternalLink>
* <InternalLink path="update-data">Update Data</InternalLink>
* <InternalLink path="delete-data">Delete Data</InternalLink>
* <InternalLink path="make-queries-fast">Optimize Statement Performance</InternalLink>
* <InternalLink path="query-behavior-troubleshooting">Troubleshoot SQL Statements</InternalLink>
* <InternalLink path="example-apps">Example Apps</InternalLink>
