transaction<R> method
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;
}
}