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" | Full serialization of concurrent transactions |
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. In Node.js 22+, you can use using for deterministic cleanup:
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
}
// Serializable transaction
const tx = db.beginTransaction('serializable');
await tx.execute("MATCH (p:Person) SET p.verified = true");
tx.commit();
// Check active state
console.log(tx.isActive); // false after commit/rollback