# begin_transaction

The begin_transaction() method for the SurrealDB Mojo SDK starts a session-scoped transaction.

Starts a session-scoped transaction and returns a handle. The handle buffers statements and flushes them on `commit()`.

```python title="Method Syntax"
client.begin_transaction(session)
```

## Arguments

<table>
    <thead>
        <tr>
            <th colspan="2" scope="col">Argument</th>
            <th colspan="2" scope="col">Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2" scope="row" data-label="Argument"><code>session</code></td>
            <td colspan="2" scope="row" data-label="Description">An optional session id.</td>
        </tr>
    </tbody>
</table>

## Handle methods

The returned handle exposes:

- `query(query, bindings_json)` to buffer a statement.
- `create(thing, content_json)` and `select(thing)` convenience wrappers.
- `commit()` to run the buffered statements atomically.
- `cancel()` to discard them.

## Example usage

```python
var txn = client.begin_transaction()
_ = txn.query("CREATE person:alice SET age = 30;")
_ = txn.query("CREATE person:bob   SET age = 31;")
txn.commit()
```

> [!NOTE]
> Session transactions run over a stateful WebSocket session. WebSocket support is rolling out. For atomic transactions over HTTP, use [`transaction_multi()`](/docs/reference/mojo/methods/transaction-multi.md).

## See also

- [Transactions](/docs/reference/mojo/concepts/transactions.md)
- [`transaction_multi()`](/docs/reference/mojo/methods/transaction-multi.md)
