Skip to content

Transactions

For the complete documentation index see: llms.txt

All documentation pages available in markdown.

Jump to the 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 enabled.

See Create and use transactions and 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.

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

Setup

import aerospike
from aerospike_helpers.operations import operations
from aerospike import exception

Connect to the Aerospike cluster.

config = {"hosts": [("127.0.0.1", 3000)], "user": "replace", "password": "replace"}
client = aerospike.client(config)
namespace = "sandbox"
set_ = "demo"

Initialize the transaction.

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

Write, batch, read, and delete inside the transaction

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

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

client.close()

Code block

Expand this section for a single code block to perform CRUD operations.
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()