# Transactions

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

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.

```c
txn = aerospike.Transaction()

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

### Write records to the cluster

```python
# Write one record.

policy = {

    "txn": txn

}

key1 = (namespace, set_, 1)

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

### Write and update records in a batch

```python
ops = [

    operations.write("c", 9999)

]

key2 = (namespace, set_, 2)

keys = [key1, key2]

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

### Read the second record

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

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

### Delete the second record

```python
# Durable delete required for deleting records using a transaction.

policy["durable_delete"] = True

client.remove(key2, policy=policy)
```

### Commit the transaction

```python
except Exception as e:

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

    client.abort(txn)

    exit(1)

print("Committing transaction", txn.id)

try:

    commitStatus = client.commit(txn)

except exception.AerospikeError as e:

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

    # Don't call abort() on the txn yet

    if e.msg.startsWith("Txn aborted:\nMark roll forward abandoned"):

        # The commit read-verify step succeeded, but the transaction monitor

        # could not be marked for roll-forward. In this case, the transaction

        # could be recommitted.

        print("Transaction can be re-committed")

        # TODO: put your own re-commit() logic here.

    else:

        # The commit read-verify step failed. The transaction has been

        # permanently aborted.

        print("Transaction aborted")

    exit(1)
```

### 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)

    # Delete the second record

    # Durable delete required for deleting records using a transaction.

    policy["durable_delete"] = True

    client.remove(key2, policy=policy)

except Exception as e:

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

    client.abort(txn)

    exit(1)

print("Committing transaction", txn.id)

try:

    commitStatus = client.commit(txn)

except exception.AerospikeError as e:

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

    # Don't call abort() on the txn yet

    if e.msg.startsWith("Txn aborted:\nMark roll forward abandoned"):

        # The commit read-verify step succeeded, but the transaction monitor

        # could not be marked for roll-forward. In this case, the transaction

        # could be recommitted.

        print("Transaction can be re-committed")

        # TODO: put your own re-commit() logic here.

    else:

        # The commit read-verify step failed. The transaction has been

        # permanently aborted.

        print("Transaction aborted")

    exit(1)

client.close()
```