transaction<T> method

Future<T> transaction<T>(
  1. Future<T> body(
    1. Transaction tx
    )
)

Initiates a nested transaction as a new savepoint. If body completes normally, the savepoint is released (changes become part of the enclosing transaction). If body throws, the savepoint is rolled back (only this nested transaction's changes are undone) and the exception is rethrown.

await db.transaction((tx) async {
  await tx.execute('INSERT INTO users(name) VALUES (?)', ['Ada']);
  try {
    await tx.transaction((inner) async {
      await inner.execute('INSERT INTO users(name) VALUES (?)', ['Bob']);
      throw StateError('oops');
    });
  } on StateError {
    // Bob's insert is rolled back; Ada's remains.
  }
});

Implementation

Future<T> transaction<T>(Future<T> Function(Transaction tx) body) {
  _ensureActive();
  return _writer.transaction(body);
}