transaction<T> method

Future<T> transaction<T>(
  1. Future<T> action(), {
  2. String? dbName,
})

Execute action inside a database transaction.

On success: commits the transaction and refreshes all streams. On error: rolls back, does NOT refresh streams, and rethrows the exception. Nested transactions are not supported and throw StateError.

Implementation

Future<T> transaction<T>(Future<T> Function() action,
    {String? dbName}) async {
  final db = _getDB(dbName);
  await db.execute("BEGIN", []);
  try {
    final result = await action();
    await db.execute("COMMIT", []);
    await updateStreams(null);
    return result;
  } catch (e) {
    await db.execute("ROLLBACK", []);
    rethrow;
  }
}