OxL Reference

method Db.transaction

Runs body in a transaction (a savepoint when nested): committed when it returns, rolled back when it panics or the task is cancelled. A retryable failure of the transaction (a serialization conflict, a busy database, a builtin:// leader change or a commit that was not applied) rolls it back and runs body AGAIN, up to DB_TX_RETRIES (3) more times, then panics with TxConflict (§11.2, D-7260..D-7263): body must have no effects outside the transaction (its emits are rows of the transaction and are rolled back with it). A nested transaction is retried by the outermost one.

Example:

oxl✓ checked in CI
use std.orm { Db }

@table("items")
data Item { @pk id: i64; name: str }

fn main() io, db {
  let db = Db.open("sqlite::memory:")!
  db.transaction(() => {
    db.insert(Item{ id: 1, name: "a" })
    db.insert(Item{ id: 2, name: "b" })
  })
  print("{db.count(Item)}")
}
Output
2
pub(api) fn transaction<R, effect F>(body: fn() -> R F) -> R  db, F

Since. 1.0.0-beta

Parameters

NameType
bodyfn() -> R F

Returns

R db, F

Effects

none declared (pure unless the body says otherwise)

Defined in: std/orm/orm.oxl:690

See also