txnAbort method

Future<void> txnAbort(
  1. Pointer<MDB_txn> txn
)

Aborts a transaction and rolls back all its changes.

After abort:

  • All changes made in the transaction are discarded
  • The transaction handle becomes invalid
  • Child transactions (if any) become invalid

Parameters:

  • txn - Transaction to abort

Example usage patterns:

// 1. In catch block (most common)
final txn = await db.txnStart();
try {
  await db.putUtf8(txn, 'key', 'value');
  await db.txnCommit(txn);
} catch (e) {
  await db.txnAbort(txn);
  rethrow;
}

// 2. Explicit rollback
final txn = await db.txnStart();
await db.putUtf8(txn, 'key', 'value');
if (shouldRollback) {
  await db.txnAbort(txn);
  return;
}
await db.txnCommit(txn);

// 3. In finally block for nested transactions
final parentTxn = await db.txnStart();
try {
  final childTxn = await db.txnStart(parent: parentTxn);
  try {
    // ... operations
    await db.txnCommit(childTxn);
  } finally {
    await db.txnAbort(childTxn);
  }
  await db.txnCommit(parentTxn);
} catch (e) {
  await db.txnAbort(parentTxn);
  rethrow;
}

Important notes:

  • The transaction handle must not be used after abort
  • Abort is safe to call multiple times (subsequent calls have no effect)
  • Abort should always be called in catch blocks
  • Child transactions must be aborted before parent transactions

Implementation

Future<void> txnAbort(Pointer<MDB_txn> txn) async {
  if (!isInitialized) throw StateError(_errDbNotInitialized);
  _lib.mdb_txn_abort(txn);
}