Skip to main content

Transactions

Group multiple write operations into an atomic unit — all succeed together or all roll back together.


Basic Pattern

The idiomatic approach uses the context manager — auto-commits on success, auto-rolls back on exception:

# Context manager (recommended)
with db.tx.begin() as tx:
leo = db.records.create(label="ACTOR", data={"name": "Leonardo DiCaprio"}, transaction=tx)
inception = db.records.create(label="MOVIE", data={"title": "Inception"}, transaction=tx)
db.records.attach(source=leo, target=inception, options={"type": "ACTED_IN"}, transaction=tx)
# committed automatically — no explicit commit() needed

Or manually for more control:

tx = db.tx.begin()
try:
movie = db.records.create(label="MOVIE", data={"title": "Inception"}, transaction=tx)
actor = db.records.create(label="ACTOR", data={"name": "Leonardo DiCaprio"}, transaction=tx)
db.records.attach(source=movie, target=actor, options={"type": "STARS_IN"}, transaction=tx)
tx.commit()
except Exception:
tx.rollback()
raise

API Reference

MethodDescription
db.tx.begin(ttl?) as ctx mgrStart transaction, auto-commit/rollback on scope exit
db.tx.begin(ttl?)Start a new transaction; returns a Transaction object
tx.commit()Persist all operations
tx.rollback()Discard all operations

Timeouts

Uncommitted transactions are automatically rolled back after the TTL expires.

ParameterValue
Default TTL5 000 ms
Maximum TTL30 000 ms
tx = db.tx.begin(ttl=15000)   # 15 s timeout

Supported Operations

All record and relationship write operations support transactions:

CategoryOperations
Recordscreate / create_many / createMany · update · set · delete / delete_by_id / deleteById
Relationshipsattach / detach
Readsfind (within transaction — reads your uncommitted writes)

See also