readTxn when several reads must share a stable snapshot, and writeTxn when a set of writes must either all succeed or all be discarded together. Single calls such as a lone put or delete don’t need an explicit transaction — reach for one only when a workflow involves more than one database step that must stay consistent.
Read Transactions
readTxn runs a callback inside a consistent read snapshot. Every query inside the callback sees the same database state — even if other writes land between them.
readTxn. Calling put, delete, or any other write operation inside a read transaction throws a CindelTransactionError.
Write Transactions
writeTxn runs writes atomically. If every operation in the callback completes without throwing, the transaction commits. If the callback throws for any reason, Cindel rolls the entire transaction back — no partial writes are ever persisted.
writeTxn when a write depends on existing data:
Rollback Behavior
When thewriteTxn callback throws, Cindel rolls the transaction back automatically. Rolled-back writes are never committed to storage, and watcher notifications for those writes are never emitted. The error is returned to the caller so you can retry, show an error, or abandon the operation.
Multi-Step Example: Checkout
The following checkout example shows a read-then-write pattern inside a singlewriteTxn. The stock check and the inventory deduction belong to the same business operation. If stock is insufficient, the callback throws and nothing is committed.
Common Mistakes
Writing inside readTxn → CindelTransactionError
Writing inside readTxn → CindelTransactionError
readTxn is for reads only. Any write call inside a read transaction throws CindelTransactionError immediately.Nesting explicit transactions → rejected
Nesting explicit transactions → rejected
Cindel rejects nested explicit transactions. If you open a Keep transaction boundaries at the outermost caller level. Helper methods should perform reads and writes without opening their own transactions, leaving that responsibility to the code that calls them.
writeTxn inside another writeTxn, the inner call is rejected.Swallowing errors inside a transaction → rollback won't fire
Swallowing errors inside a transaction → rollback won't fire
Rollback depends on the callback throwing. If you catch an error inside the callback and don’t rethrow it, Cindel treats the callback as successful and commits whatever ran before the catch.