Skip to content

Commit a80d87d

Browse files
committed
docs(client): make the transaction examples self-contained
The transaction snippets referenced a `db` whose defining `with` block from the Quick Start had already closed by the time a reader reaches them in the same interpreter, and pasted independently they had no client at all. Each example now opens its own client context, so it runs exactly as shown.
1 parent 01e93f2 commit a80d87d

1 file changed

Lines changed: 23 additions & 15 deletions

File tree

README.md

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,18 @@ with CoordinodeClient("localhost:7080") as db:
6666
## Transactions
6767

6868
`db.cypher(...)` commits each statement on its own. To make several statements
69-
land together, or not at all, run them in a transaction:
69+
land together, or not at all, run them in a transaction (each snippet opens
70+
its own client, so it runs as pasted):
7071

7172
```python
72-
with db.transaction() as tx:
73-
tx.cypher("CREATE (:Person {name: $n})", params={"n": "Alice"})
74-
tx.cypher("CREATE (:Person {name: $n})", params={"n": "Bob"})
75-
# commits here; an exception anywhere in the block rolls back instead,
76-
# leaving neither person in the database
73+
from coordinode import CoordinodeClient
74+
75+
with CoordinodeClient("localhost:7080") as db:
76+
with db.transaction() as tx:
77+
tx.cypher("CREATE (:Person {name: $n})", params={"n": "Alice"})
78+
tx.cypher("CREATE (:Person {name: $n})", params={"n": "Bob"})
79+
# commits here; an exception anywhere in the block rolls back
80+
# instead, leaving neither person in the database
7781
```
7882

7983
The same surface is on `AsyncCoordinodeClient`, with `async with` and awaited
@@ -82,15 +86,19 @@ statements. When the commit point sits outside a block, drive it by hand:
8286
```python
8387
from contextlib import suppress
8488

85-
tx = db.begin_transaction()
86-
try:
87-
tx.cypher("MERGE (n:Entity {name: $n})", params={"n": "Alice"})
88-
applied_index = tx.commit()
89-
except Exception:
90-
# Suppressed so a failing rollback cannot replace the error that caused it.
91-
with suppress(Exception):
92-
tx.rollback()
93-
raise
89+
from coordinode import CoordinodeClient
90+
91+
with CoordinodeClient("localhost:7080") as db:
92+
tx = db.begin_transaction()
93+
try:
94+
tx.cypher("MERGE (n:Entity {name: $n})", params={"n": "Alice"})
95+
applied_index = tx.commit()
96+
except Exception:
97+
# Suppressed so a failing rollback cannot replace the error that
98+
# caused it.
99+
with suppress(Exception):
100+
tx.rollback()
101+
raise
94102
```
95103

96104
Requires a CoordiNode server of **v0.5.5 or newer** — the release this client

0 commit comments

Comments
 (0)