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.
Applies to
Transactions require Go client 8.0.0 and later, Aerospike Database 8.0.0 and later, and a strong-consistency namespace.
See Create and use transactions for database concepts and error handling patterns.
CRUD operations in a transaction
The following example demonstrates create, read, update, and delete operations within a transaction.
Setup
Connect to the Aerospike cluster.
import ( "fmt" "log"
as "github.com/aerospike/aerospike-client-go/v8")
client, err := as.NewClient("127.0.0.1", 3000)if err != nil { log.Fatal(err)}defer client.Close()
namespace := "sandbox"setName := "demo"Create a transaction.
txn := as.NewTxn()fmt.Printf("Begin txn: %d\n", txn.Id())Write records
Attach the transaction to a write policy, then write records inside the transaction.
writePolicy := as.NewWritePolicy(0, 0)writePolicy.Txn = txn
key1, err := as.NewKey(namespace, setName, 1)if err != nil { log.Fatal(err)}
if err := client.PutBins(writePolicy, key1, as.NewBin("a", 1)); err != nil { client.Abort(txn) log.Fatalf("Put failed: %v", err)}
key2, err := as.NewKey(namespace, setName, 2)if err != nil { log.Fatal(err)}
if err := client.PutBins(writePolicy, key2, as.NewBin("b", 2)); err != nil { client.Abort(txn) log.Fatalf("Put failed: %v", err)}Read a record
Attach the transaction to a read policy.
readPolicy := as.NewPolicy()readPolicy.Txn = txn
record, err := client.Get(readPolicy, key2, "b")if err != nil { client.Abort(txn) log.Fatalf("Get failed: %v", err)}fmt.Printf("2nd record bins: %v\n", record.Bins)Update a record
Attach the transaction to a write policy, then update a bin inside the transaction.
if err := client.PutBins(writePolicy, key1, as.NewBin("a", 100)); err != nil { client.Abort(txn) log.Fatalf("Update failed: %v", err)}Delete a record
You must set DurableDelete to true when deleting records inside a transaction. See Durable deletes.
deletePolicy := as.NewWritePolicy(0, 0)deletePolicy.Txn = txndeletePolicy.DurableDelete = true
if _, err := client.Delete(deletePolicy, key2); err != nil { client.Abort(txn) log.Fatalf("Delete failed: %v", err)}Commit the transaction
if status, err := client.Commit(txn); err != nil { log.Fatalf("Commit failed: %v", err)} else { fmt.Printf("Commit status: %v\n", status)}If a command fails before commit, call client.Abort(txn) to roll back.
Code block
Expand this section for a single code block to perform CRUD operations in a transaction.
package main
import ( "fmt" "log"
as "github.com/aerospike/aerospike-client-go/v8")
func main() { client, err := as.NewClient("127.0.0.1", 3000) if err != nil { log.Fatal(err) } defer client.Close()
namespace := "sandbox" setName := "demo"
txn := as.NewTxn() fmt.Printf("Begin txn: %d\n", txn.Id())
writePolicy := as.NewWritePolicy(0, 0) writePolicy.Txn = txn
key1, err := as.NewKey(namespace, setName, 1) if err != nil { log.Fatal(err) }
if err := client.PutBins(writePolicy, key1, as.NewBin("a", 1)); err != nil { client.Abort(txn) log.Fatalf("Put failed: %v", err) }
key2, err := as.NewKey(namespace, setName, 2) if err != nil { log.Fatal(err) }
if err := client.PutBins(writePolicy, key2, as.NewBin("b", 2)); err != nil { client.Abort(txn) log.Fatalf("Put failed: %v", err) }
readPolicy := as.NewPolicy() readPolicy.Txn = txn
record, err := client.Get(readPolicy, key2, "b") if err != nil { client.Abort(txn) log.Fatalf("Get failed: %v", err) } fmt.Printf("2nd record bins: %v\n", record.Bins)
if err := client.PutBins(writePolicy, key1, as.NewBin("a", 100)); err != nil { client.Abort(txn) log.Fatalf("Update failed: %v", err) }
deletePolicy := as.NewWritePolicy(0, 0) deletePolicy.Txn = txn deletePolicy.DurableDelete = true
if _, err := client.Delete(deletePolicy, key2); err != nil { client.Abort(txn) log.Fatalf("Delete failed: %v", err) }
if status, err := client.Commit(txn); err != nil { log.Fatalf("Commit failed: %v", err) } else { fmt.Printf("Commit status: %v\n", status) }}