Class Transaction

java.lang.Object
com.surrealdb.Native
com.surrealdb.Transaction
All Implemented Interfaces:
AutoCloseable

public class Transaction extends Native implements AutoCloseable
Client-side transaction. Use Surreal.beginTransaction() to start a transaction. Operations (e.g. query(String)) run within the transaction until commit() or cancel() is called. With try-with-resources, a transaction that was not committed is cancelled:
try (Transaction txn = db.beginTransaction()) {
	txn.query("CREATE person:tobie");
	txn.commit();
}
  • Method Details

    • commit

      public void commit()
      Commits the transaction. After this call, the transaction is completed and must not be used. On failure the native layer throws; the return value is only true on success.
    • cancel

      public void cancel()
      Cancels (rolls back) the transaction. After this call, the transaction is completed and must not be used. On failure the native layer throws; the return value is only true on success.
    • close

      public void close()
      Cancels the transaction if it was neither committed nor cancelled, so a try-with-resources block rolls back unless commit() was called. After commit() or cancel(), this does nothing.
      Specified by:
      close in interface AutoCloseable
      Overrides:
      close in class Native
    • query

      public Response query(String sql)
      Runs a SurrealQL query within this transaction.
      Parameters:
      sql - the SurrealQL query
      Returns:
      the query response
    • query

      public Response query(String sql, Map<String,?> params)
      Runs a parameterized SurrealQL query within this transaction.
      Parameters:
      sql - the SurrealQL query
      params - a map containing parameter values to be bound to the SQL query
      Returns:
      the query response