Skip to main content
Transactions group database work into one consistent operation. Use 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.
Single put and delete calls are already atomic on their own. You only need an explicit transaction when multiple operations must commit or roll back as a unit.

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.
Do not attempt any writes inside 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.
Use a write transaction whenever a set of changes must commit as one logical unit:
You can also read inside a writeTxn when a write depends on existing data:

Rollback Behavior

When the writeTxn 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 single writeTxn. 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

readTxn is for reads only. Any write call inside a read transaction throws CindelTransactionError immediately.
Cindel rejects nested explicit transactions. If you open a writeTxn inside another writeTxn, the inner call is rejected.
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.
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.
Transactions hold a database lock for their duration. Waiting on network calls, showing UI prompts, or running any long-running non-database work inside a transaction callback blocks other database operations and increases the risk of contention.Prepare all inputs before entering the transaction, then perform only the database reads and writes that must be atomic inside the callback.