Skip to content
Webinar - May 13th: How Criteo powers real-time decisions with a reduced footprintRegister now

Transactions

Jump to the Code block for a combined complete example.

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 records to the cluster

# 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

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)

Commit the transaction

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

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

Was this page helpful?

What type of feedback are you giving?

What would you like us to know?

+Capture screenshot

Can we reach out to you?