---
title: "Transactions"
description: "Guide to performing CRUD operations using transactions in the Aerospike Python client."
---

# Transactions

> For the complete documentation index see: [llms.txt](https://aerospike.com/docs/llms.txt)
> 
> All documentation pages available in markdown.

Jump to the [Code block](#code-block) for a combined complete example.

Transactions require:

-   Python client 16.0.0 and later (multi-record transaction API).
-   Aerospike Database 8.0.0 and later.
-   A namespace with [strong consistency](https://aerospike.com/docs/database/manage/namespace/consistency) enabled.

See [Create and use transactions](https://aerospike.com/docs/database/learn/transactions) and [Failed commits](https://aerospike.com/docs/database/learn/transactions#failed-commits) for commit failure handling. Python client 19.3.0 adds `aerospike.TXN_STATE_COMMIT_FAILED` for status checks instead of parsing error message text.

::: caution
A commit is **in doubt** when the client cannot tell whether the server finished the commit. If a commit is in doubt, retry `client.commit(txn)` on the same transaction object instead of starting a new transaction. If a recommit is still in doubt, log the affected keys for manual verification. Do not assume failure and retry the whole transaction; that can duplicate writes. When `commit()` fails and the error is not in doubt, retry the **entire** transaction per [Failed commits](https://aerospike.com/docs/database/learn/transactions#failed-commits).
:::

The following example demonstrates create, update, read, and delete operations within a transaction.

### Setup

```python
import aerospike

from aerospike_helpers.operations import operations

from aerospike import exception
```

Connect to the Aerospike cluster.

```python
config = {"hosts": [("127.0.0.1", 3000)], "user": "replace", "password": "replace"}

client = aerospike.client(config)

namespace = "sandbox"

set_ = "demo"
```

Initialize the transaction.

```python
txn = aerospike.Transaction()

print("Transaction ID is", txn.id)
```

### Write, batch, read, and delete inside the transaction

```python
try:

    policy = {"txn": txn}

    key1 = (namespace, set_, 1)

    client.put(key1, bins={"a": 1}, policy=policy)

    ops = [operations.write("c", 9999)]

    key2 = (namespace, set_, 2)

    keys = [key1, key2]

    client.batch_operate(keys, ops, policy_batch=policy)

    _, _, bins = client.get(key2, policy=policy)

    print("2nd record bins:", bins)

    # Durable delete is required; without it the delete may not commit correctly in the transaction.

    policy["durable_delete"] = True

    client.remove(key2, policy=policy)

except Exception as e:

    print("Command during transaction failed:", e)

    client.abort(txn)

    raise
```

### Commit the transaction

```python
print("Committing transaction", txn.id)

try:

    client.commit(txn)

    print("Transaction committed successfully")

except exception.AerospikeError as e:

    print("commit() failed:", e)

    if e.in_doubt:

        try:

            client.commit(txn)

            print("Transaction committed successfully")

        except exception.AerospikeError as e2:

            if e2.in_doubt:

                print("Recommit still in doubt. Log keys for manual verification.")

            else:

                # Not in doubt after recommit. Retry the entire transaction.

                raise

    else:

        # Not in doubt: verify-fail or other commit failure. Retry the entire transaction.

        raise

except Exception:

    print("Non-Aerospike error during commit. Transaction state may be uncertain.")

    raise
```

### Close the client connection

```python
client.close()
```

## Code block

Expand this section for a single code block to perform CRUD operations.

```python
import aerospike

from aerospike_helpers.operations import operations

from aerospike import exception

config = {"hosts": [("127.0.0.1", 3000)], "user": "replace", "password": "replace"}

client = aerospike.client(config)

namespace = "test"

set_ = "demo"

txn = aerospike.Transaction()

print("Transaction ID is", txn.id)

try:

    # Write one record.

    policy = {

        "txn": txn

    }

    key1 = (namespace, set_, 1)

    client.put(key1, bins={"a": 1}, policy=policy)

    # Write / update records in a batch

    ops = [

        operations.write("c", 9999)

    ]

    key2 = (namespace, set_, 2)

    keys = [key1, key2]

    client.batch_operate(keys, ops, policy_batch=policy)

    # Read the second record

    _, _, bins = client.get(key2, policy=policy)

    print("2nd record bins:", bins)

    # Durable delete is required; without it the delete may not commit correctly in the transaction.

    policy["durable_delete"] = True

    client.remove(key2, policy=policy)

except Exception as e:

    print("Command during transaction failed:", e)

    client.abort(txn)

    raise

print("Committing transaction", txn.id)

try:

    client.commit(txn)

    print("Transaction committed successfully")

except exception.AerospikeError as e:

    print("commit() failed:", e)

    if e.in_doubt:

        try:

            client.commit(txn)

            print("Transaction committed successfully")

        except exception.AerospikeError as e2:

            if e2.in_doubt:

                print("Recommit still in doubt. Log keys for manual verification.")

            else:

                # Not in doubt after recommit. Retry the entire transaction.

                raise

    else:

        # Not in doubt: verify-fail or other commit failure. Retry the entire transaction.

        raise

except Exception:

    print("Non-Aerospike error during commit. Transaction state may be uncertain.")

    raise

client.close()
```