transaction<T> method

  1. @override
Future<T> transaction<T>(
  1. void f(
    1. DbTransaction tx,
    2. void setResult(
      1. T res
      )
    )
)
override

Runs the provided non-async function inside a transaction.

The function may not use async/await otherwise the transaction may commit before the queries are actually executed. This is a limitation of some adapters, that the function passed to the transaction runs "synchronously" through callbacks without releasing the event loop.

Implementation

@override
Future<T> transaction<T>(
  void Function(DbTransaction tx, void Function(T res) setResult) f,
) async {
  final completer = Completer<T>();

  return db.transaction(() {
    final tx = Transaction(this, (e) {
      completer.completeError(e);
    });

    runZonedGuarded(() {
      f(tx, (T res) {
        completer.complete(res);
      });
    }, (error, stack) {
      completer.completeError(error);
    });

    return completer.future;
  });
}