Transactions

PARTIAL

A change to an ehgraph namespace commits as one graph_version, or it does not commit. Several statements can share that commit. This is ehgraph at git f200516, and the page is PARTIAL because that revision has no release tag and the server embeds it behind a default-off switch (15.1). The element versions these commits check are the model's version_updated (15.2). The Cypher subset that runs inside a transaction is 15.3.

Writers that send a plain graph.upsert or graph.deactivate with no expect keep the same path, the same versions, and the same responses (docs/wiki/Transactions.md).

KindWhereWhat it guarantees
Conditional commit (expect)graph.upsert, graph.deactivate by ids, graph.cypher, graph.tx.commit, Query API commit, Bolt BEGIN with tx_metadata.expectApplies only while every expected element version, and the graph version if you sent one, still holds. Otherwise VERSION_CONFLICT (409) and nothing is written
Auto-commit Cypher writegraph.cypher, POST /db/{ns}/query/v2, Bolt RUN outside BEGINOne serializable graph version for every upsert and deactivation in the statement. A lost race re-runs on the new head
Multi-statement transactiongraph.tx.begin / run / commit / rollback, Query API /tx, Bolt BEGIN / RUN / COMMIT / ROLLBACKSnapshot reads plus read-your-writes. Every statement commits as one graph version, serializably, or none of them do

Conditional commits: expect

"expect": {
  "graph_version": 9121,
  "versions": {"gw:1": 9121, "lock:job-7": 0}
}

graph_version is optional: the namespace head must equal it, and 0 means the namespace must be absent. Each entry in versions is that element's version_updated. 0 means the element must not exist (create-if-absent). A deactivation bumps version_updated, so a concurrent delete fails a stale expectation.

The check runs under the store's write lock, in the same critical section that appends the commit. An unknown key inside expect is rejected (400). A server from before this feature ignores an unknown top-level expect, so a caller that depends on it checks graph.instance.info: features must contain "expect".

Several statements, one commit

graph.tx.begin     {namespace, timeout_ms?, source?, execution_id?}
                   → {tx_id, graph_version, expires_ms}
graph.tx.run       {tx_id, query, params?, max_rows?}
                   → {columns, rows, stats, expires_ms}
graph.tx.commit    {tx_id, expect?, idempotency_key?}
                   → {graph_version, changed, writes}
graph.tx.rollback  {tx_id} → {rolled_back}

The Neo4j Query API uses the same semantics (docs/wiki/Transactions.md):

RequestEffect
POST /db/{ns}/query/v2/txOpen. 202 with transaction.id and transaction.expires. An optional statement runs immediately
POST /db/{ns}/query/v2/tx/{id}Run one statement in that transaction
POST /db/{ns}/query/v2/tx/{id}/commitCommit. ehgraph accepts an expect extension. 202 with bookmarks: ["ehgraph:{ns}:{version}"]
DELETE /db/{ns}/query/v2/tx/{id}Roll back. 404 TransactionNotFound if it is already gone

On Bolt, BEGIN opens the transaction (the database name is the namespace, tx_timeout is honoured, and tx_metadata.expect is the conditional commit). COMMIT returns a bookmark. ROLLBACK, RESET, GOODBYE, and a dropped connection all discard it.

The 6×15=90 figure was recounted, not re-run

docs/wiki/Transactions.md records a check with the official Python driver 5.28.1: 6 threads, each doing 15 read-modify-write increments, finished at exactly 90, and the driver retried the conflicts. This chapter recounted that product from the sentence (6×15=90). The driver was not re-run against f200516 for this page, so the 90 is the wiki's recorded result, not a new measurement.

What a transaction can see

BEGIN pins the namespace head. Every statement reads that snapshot plus the transaction's own earlier writes. Commits from other writers stay invisible inside the transaction.

Nothing reaches the store before COMMIT. Writes live in a copy-on-write overlay. ROLLBACK frees that memory and does not touch the log. A failed statement fails the transaction, as in Neo4j: later statements and COMMIT are refused, and only ROLLBACK is accepted. The earlier writes are discarded with it.

COMMIT sends the net effect — upserts with full property images, plus soft deactivations — to Store::transact, which appends one change-log frame and so one graph_version. Schema rules and "identical content does not bump the version" apply as they do for graph.upsert. A transaction whose net effect is empty commits without a new version.

The commit is serializable optimistic concurrency: first committer wins. While the transaction runs it records a footprint:

It recordedWhenA later commit conflicts if it…
Element idsAny lookup, found or not, and every element writtenChanged that element
LabelsA label scan MATCH (n:L)Created or changed any L node
"All nodes"A scan with no label, MATCH (n)Changed any node
Adjacency of node xAn expansion from xCreated, changed, or removed an edge at x

Under the write lock, COMMIT looks at elements with version_updated above the snapshot. If none of them intersects the footprint, the transaction's reads are still what the head would return, and committing now is equivalent to having run it serially at the head. If one intersects, the commit fails with VERSION_CONFLICT and a message that starts serialization conflict: and names the element. Nothing is written. The client re-runs the whole transaction. If the head has not moved, the scan is skipped.

The check is per element, per label, and per adjacency. It is not per property: a concurrent write to a different property of a node you read still conflicts. Writes to elements you neither read nor scanned do not. The wiki's example is a topology writer updating access points while a transaction edits Account nodes.

An auto-commit Cypher write uses the same commit. A lost race re-runs the statement on the new head, up to 3 attempts (DEFAULT, crates/ehgraph-api/src/dispatch.rs:965, AUTOCOMMIT_ATTEMPTS), and then returns VERSION_CONFLICT. The caller sees only the committed attempt. Older auto-commit results reported upserts and deactivations as two versions. At f200516 they are one: writes: [{"op": "commit", …}], or [] when nothing changed.

Timeouts, grants, and the error codes

The idle timeout is 30 seconds by default and at most 10 minutes (DEFAULT, crates/ehgraph-api/src/tx.rs:42 and :44, DEFAULT_TX_TIMEOUT_MS and MAX_TX_TIMEOUT_MS). Bolt's tx_timeout uses the same bound. Each statement resets the idle timer. At most 256 transactions are open per instance (tx.rs:40, MAX_OPEN_TXS). A transaction belongs to the principal that opened it. BEGIN checks the read grant. An updating statement, and a COMMIT that writes, check the write grant. An open transaction keeps its snapshot resident until it ends.

ehgraphHTTPNeo4j code on Bolt and the Query APIWhat the client does
VERSION_CONFLICT "serialization conflict: …"409Neo.TransientError.Transaction.OutdatedRe-run the whole transaction. execute_write does this
VERSION_CONFLICT "expectation failed: …"409Neo.ClientError.Schema.ConstraintValidationFailedRe-read the versions in expect, then retry
NOT_FOUND "transaction … not found"404Neo.ClientError.Transaction.TransactionNotFoundStop. It expired, committed, or rolled back

ehgraph already serializes commits through one store lock and already keeps version_updated on each element, so the footprint check needs no lock table. A change-log frame could already hold a mix of upserts and deactivations, so transactions do not change the on-disk format. Older builds still read the log.

Check your understanding