11.2 Database transaction
atomic(db) {
db.insert(order)
db.update(User, uid) { ordersCount += 1 }
emit OrderEvents(OrderEvent.Created(order.id))
}- The body is executed in a transaction of the database
db. The isolation level isserializablefor the built-in storage; for external databases —@isolation(level)or the driver's value. - Inside the body only the effect
dbfor this database andemitare allowed. The effectsnet,ioand other databases are a compile error; fornetandiothe diagnostic code isatomic-db-effects(the effectstimeandranddo not leave the process and are allowed).
Implementation-defined. Another database in the body (D-8064; D-7790). This implementation cannot tell another database by the effect set: using another database inside
atomic(db)is not diagnosed.
- A serialisation conflict causes an automatic retry of the body up to
DB_TX_RETRIEStimes (a cluster value, 3 by default) within the time budget, then a panicTxConflict.atomic(db)keeps the type of the body and does not addTxConflictto the result type (an edition 1.0 correction: formerly "an error", D-8052; DIV-012; D-7260..D-7262). These values are set incluster { defaults { db_tx_retries n db_tx_retry_budget d } }; the environment variablesOXL_DB_TX_RETRIESandOXL_DB_TX_RETRY_BUDGET_MSoverride the cluster value (D-8064; D-7780). emitwrites the event to the outbox table of the same database; after commit a relay publishes it to the topic (at-least-once).
Implementation-defined. Outbox by database kind (D-8055; DIV-014; D-4999, D-5484). This implementation: the outbox works for
builtin://(including on a node without the local shard leader); forsqlite:andpostgres:and for transactions on remote shardsemitinsideatomic(db)is not supported (an optional feature).
- In the built-in storage a transaction MUST touch one sharding key;
atomic(db, cross_shard: true)permits several shards at the cost of a two-phase commit.
Implementation-defined. Two-phase commit across shards (D-8055; DIV-021; D-4410). An optional feature. This implementation contains no 2PC: the parameter
cross_shard: trueis accepted with the warningcross-shard-tx, and the body is executed as an ordinary transaction; a request that goes beyond one shard fails at run time with the errorCrossShardTx. The warningcross-shard-txis also issued whenfind/deleteByIdof sharded tables in one body use different literal keys; keys that are variables cannot be compared before the run (D-8064; D-7790).
Related diagnostics
atomic-db-effects, cross-shard-tx
See also
Source: docs/spec/oxl-standard.en.md