# begin

The .begin() method on the SurrealDB Rust SDK client starts a multi-statement transaction and returns a handle for running queries, commits, and rollbacks.

Starts a transaction. The connection is taken into a [`Transaction`](https://docs.rs/surrealdb/latest/surrealdb/method/struct.Transaction.html) that exposes the same query and CRUD entry points as `Surreal` (scoped to the transaction) plus [`commit()`](https://docs.rs/surrealdb/latest/surrealdb/method/struct.Transaction.html#method.commit) and [`cancel()`](https://docs.rs/surrealdb/latest/surrealdb/method/struct.Transaction.html#method.cancel).

Note that this method takes by value (taking a `self`), which is then passed on to the `Transaction`. The `.commit()` and `.cancel()` methods are used to finalise the transaction and return the `Surreal` client for reuse.

On a remote WebSocket server, each open client-managed transaction counts toward [`SURREAL_MAX_TRANSACTIONS_PER_CONNECTION`](/docs/reference/cli/surrealdb-cli/environment-variables.md#websocket-config) (connection default session) or [`SURREAL_MAX_TRANSACTIONS_PER_SESSION`](/docs/reference/cli/surrealdb-cli/environment-variables.md#websocket-config) (attached session). Exceeding the limit returns `Too many open transactions`. Detach/`reset` cancel that session's open transactions.

```rust title="Method Syntax"
let tx = db.begin().await?;
// tx.query(...), .select(), .create(), .insert(), .upsert(), .update(), .delete()
```

## `.commit()`

[`tx.commit().await?`](https://docs.rs/surrealdb/latest/surrealdb/method/struct.Transaction.html#method.commit) applies every statement run on the handle and returns ownership of the underlying `Surreal` client so the connection can run further work outside the transaction.

```rust
// let db = tx.commit().await?;
```

## `.cancel()`

[`tx.cancel().await?`](https://docs.rs/surrealdb/latest/surrealdb/method/struct.Transaction.html#method.cancel) rolls back the transaction and **also** returns the `Surreal` client for reuse.

```rust
// let db = tx.cancel().await?;
```

For a broader discussion and more detailed examples, see the concept page on [Manual transactions](/docs/reference/rust/concepts/transaction.md).

### See also

* [`.begin()` on Docs.rs](https://docs.rs/surrealdb/latest/surrealdb/struct.Surreal.html#method.begin)
* [`Transaction` in the Rust API](https://docs.rs/surrealdb/latest/surrealdb/method/struct.Transaction.html)
