Transaction¶
A database transaction with explicit commit/rollback. Transactions provide ACID guarantees for groups of operations.
Creating a Transaction¶
| Isolation Level | Description |
|---|---|
"snapshot" | Default. Reads see a consistent snapshot at transaction start |
"read_committed" | Reads see the latest committed data |
"serializable" | Not available yet: refused for now: snapshot isolation is the strongest level until queries record their reads |
Methods¶
execute()¶
Execute a query within this transaction.
const tx = db.beginTransaction();
await tx.execute("INSERT (:Person {name: 'Alix'})");
await tx.execute(
"INSERT (:Person {name: $name})",
{ name: 'Gus' }
);
Throws if the transaction is no longer active (already committed or rolled back).
commit()¶
Commit the transaction, making all changes permanent.
Throws if already committed or rolled back.
rollback()¶
Roll back the transaction, discarding all changes.
Throws if already committed or rolled back.
Properties¶
| Property | Type | Description |
|---|---|---|
isActive | boolean | Whether the transaction is still active |
Auto-rollback¶
If a transaction is dropped (garbage collected) without being committed or rolled back, it automatically rolls back. Where using is supported (Node.js 24+, or TypeScript compiled to an older target), it rolls back at the end of its block through tx[Symbol.dispose]():
using tx = db.beginTransaction();
await tx.execute("INSERT (:Person {name: 'Alix'})");
tx.commit();
// auto-rollback if commit not called
Example¶
// Basic transaction
const tx = db.beginTransaction();
try {
await tx.execute("INSERT (:Person {name: 'Alix'})");
await tx.execute("INSERT (:Person {name: 'Gus'})");
tx.commit(); // Both inserts committed atomically
} catch (e) {
tx.rollback(); // Discard all changes on error
}
// Read committed transaction
const tx = db.beginTransaction('read_committed');
await tx.execute("MATCH (p:Person) SET p.verified = true");
tx.commit();
// Check active state
console.log(tx.isActive); // false after commit/rollback