txnCommit method

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

Commits a transaction and makes all its changes permanent.

After a successful commit:

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

Parameters:

  • txn - Transaction to commit

Example with error handling:

final txn = await db.txnStart();
try {
  // Perform multiple operations in one transaction
  await db.putUtf8(txn, 'user:1', '{"name": "John"}');
  await db.putUtf8(txn, 'user:2', '{"name": "Jane"}');

  // Make all changes permanent
  await db.txnCommit(txn);
} catch (e) {
  // On any error, abort the transaction
  await db.txnAbort(txn);
  rethrow;
}

Important notes:

  • The transaction handle must not be used after commit
  • Always use try-catch with txnAbort in the catch block
  • Commit is atomic - either all changes succeed or none do

Throws StateError if database is not initialized Throws LMDBException if commit fails

Implementation

Future<void> txnCommit(Pointer<MDB_txn> txn) async {
  if (!isInitialized) throw StateError(_errDbNotInitialized);

  final result = _lib.mdb_txn_commit(txn);
  if (result != 0) {
    throw LMDBException('Failed to commit transaction', result);
  }
}