Skip to content

Latest commit

 

History

History
83 lines (56 loc) · 3.06 KB

commit-transaction.md

File metadata and controls

83 lines (56 loc) · 3.06 KB
title summary toc
COMMIT
Commit a transaction with the COMMIT statement in CockroachDB.
true

The COMMIT statement commits the current transaction or, when using advanced client-side transaction retries, clears the connection to allow new transactions to begin.

When using advanced client-side transaction retries, statements issued after SAVEPOINT are committed when RELEASE SAVEPOINT is issued instead of COMMIT. However, you must still issue a COMMIT statement to clear the connection for the next transaction.

For non-retryable transactions, if statements in the transaction generated any errors, COMMIT is equivalent to ROLLBACK, which aborts the transaction and discards all updates made by its statements.

Synopsis

{% include {{ page.version.version }}/sql/diagrams/commit_transaction.html %}

Required privileges

No privileges are required to commit a transaction. However, privileges are required for each statement within a transaction.

Aliases

In CockroachDB, END is an alias for the COMMIT statement.

Example

Commit a transaction

How you commit transactions depends on how your application handles transaction retries.

Client-side retryable transactions

When using advanced client-side transaction retries, statements are committed by RELEASE SAVEPOINT. COMMIT itself only clears the connection for the next transaction.

{% include copy-clipboard.html %}

> BEGIN;

{% include copy-clipboard.html %}

> SAVEPOINT cockroach_restart;

{% include copy-clipboard.html %}

> UPDATE products SET inventory = 0 WHERE sku = '8675309';

{% include copy-clipboard.html %}

> INSERT INTO orders (customer, sku, status) VALUES (1001, '8675309', 'new');

{% include copy-clipboard.html %}

> RELEASE SAVEPOINT cockroach_restart;

{% include copy-clipboard.html %}

> COMMIT;

{{site.data.alerts.callout_danger}}This example assumes you're using client-side intervention to handle transaction retries.{{site.data.alerts.end}}

Automatically retried transactions

If you are using transactions that CockroachDB will automatically retry (i.e., all statements sent in a single batch), commit the transaction with COMMIT.

{% include copy-clipboard.html %}

> BEGIN; UPDATE products SET inventory = 100 WHERE = '8675309'; UPDATE products SET inventory = 100 WHERE = '8675310'; COMMIT;

See also