Transactions
The Java SDK supports client-side transactions for grouping multiple queries into an atomic unit. All queries within a Transaction are isolated from other operations and are either applied together on commit or discarded entirely on cancel.
API References
| Method | Description |
|---|---|
db.beginTransaction() | Starts a new transaction |
tx.query(sql) | Executes a query within the transaction |
tx.commit() | Commits the transaction |
tx.cancel() | Cancels (rolls back) the transaction |
Starting a transaction
The .beginTransaction() method returns a Transaction object. All queries executed through this object are isolated until the transaction is committed or cancelled.
Executing queries in a transaction
The tx.query() method works like db.query() but executes within the transaction scope. Each call returns a Response that you can inspect immediately, but the underlying changes are not visible outside the transaction until it is committed.
Note
Committing a transaction
Call .commit() to apply all changes atomically. Once committed, the changes become visible to other connections and queries.
Cancelling a transaction
Call .cancel() to discard all changes made within the transaction. Use a try-catch pattern to ensure the transaction is rolled back if any query fails.
Learn more
Transaction API reference for complete method signatures
Executing queries for query execution outside transactions
Error handling for handling transaction errors
SurrealQL BEGIN for server-side transaction syntax
SurrealQL COMMIT and CANCEL for transaction control