SurrealTransaction represents an in-progress transaction. It is itself a queryable, so the CRUD builders are available scoped to the transaction. Transactions are started with the extension functions on a session and require the WebSocket transport.
Source: surrealdb.kotlin
import com.surrealdb.kotlin.beginTransaction
import com.surrealdb.kotlin.transaction session.transaction { }
Runs the block against a new SurrealTransaction, committing it if the block returns normally and cancelling it if the block throws.
session.transaction { /* ... */ }Parameter | Type | Description |
|---|---|---|
block | suspend SurrealTransaction.() -> Unit | The operations to run inside the transaction. |
Returns: Unit
client.transaction {
create(RecordId("person", "tx"))
.content(buildJsonObject { put("name", "Tx") })
.await()
} session.beginTransaction()
Begins a transaction explicitly and returns a SurrealTransaction for manual commit or cancel.
session.beginTransaction()Returns: SurrealTransaction
val tx = client.beginTransaction()
try {
tx.create(Table("person")).content(buildJsonObject { put("name", "Ada") }).await()
tx.commit()
} catch (cause: Throwable) {
tx.cancel()
throw cause
}Methods
.commit()
Commits the transaction, persisting all its operations.
tx.commit()Returns: Unit
.cancel()
Cancels the transaction, discarding all its operations.
tx.cancel()Returns: Unit
Properties
Property | Type | Description |
|---|---|---|
txnId | String | The transaction's identifier. |