transaction method

Future<void> transaction(
  1. Future<void> action(
    1. TransactionCrdt txn
    )
)

Initiates a transaction in this database. Caution: calls to the parent crdt inside a transaction block will result in a deadlock.

await database.transaction((txn) async {
  // OK
  await txn.execute('SELECT * FROM users');

  // NOT OK: calls to the parent crdt in a transaction
  // The following code will deadlock
  await crdt.execute('SELECT * FROM users');
});

Implementation

Future<void> transaction(
    Future<void> Function(TransactionCrdt txn) action) async {
  late final TransactionCrdt transaction;
  await _db.transaction((txn) async {
    transaction = TransactionCrdt(txn, canonicalTime.increment());
    await action(transaction);
  });
  // Notify on changes
  if (transaction.affectedTables.isNotEmpty) {
    await onDatasetChanged(
        transaction.affectedTables, transaction.canonicalTime);
  }
}