transaction<R> method

  1. @override
Future<R> transaction<R>(
  1. Future<R> body(
    1. Connection tx
    )
)
override

Runs body inside a transaction: commits when it returns, rolls back and rethrows when it throws. Nested calls use savepoints.

Implementation

@override
Future<R> transaction<R>(Future<R> Function(Connection tx) body) async {
  final savepoint = _depth == 0 ? null : 'sp$_depth';
  await execute(savepoint == null ? 'begin' : 'savepoint $savepoint');
  _depth++;
  try {
    final result = await body(this);
    _depth--;
    await execute(
      savepoint == null ? 'commit' : 'release savepoint $savepoint',
    );
    return result;
  } catch (_) {
    _depth--;
    await execute(
      savepoint == null ? 'rollback' : 'rollback to savepoint $savepoint',
    );
    rethrow;
  }
}