Transactions group multiple operations so they either all succeed or all fail together. The Kotlin SDK exposes transactions as extension functions on a session: a block form that commits or cancels automatically, and an explicit form for manual control.
Note
API references
| Method | Description |
|---|---|
session.transaction { } | Runs a block, committing or cancelling automatically |
session.beginTransaction() | Begins a transaction explicitly |
tx.commit() | Commits the transaction |
tx.cancel() | Cancels the transaction |
Block form
The transaction { } builder runs your block against a SurrealTransaction, commits it if the block returns normally, and cancels it if the block throws. The transaction is itself a queryable, so all the CRUD builders are available scoped to it.
Explicit form
For finer control, begin a transaction with .beginTransaction() and commit or cancel it yourself.
Learn more
Transaction reference for the full API
Multiple sessions — transactions run within a session
SurrealQL transactions for transaction semantics