runInTransaction<T extends Object> abstract method

Future<Result<T>> runInTransaction<T extends Object>(
  1. String connectionId,
  2. Future<Result<T>> action(
    1. int txnId
    ), {
  3. IsolationLevel? isolationLevel,
  4. SavepointDialect? savepointDialect,
  5. TransactionAccessMode? accessMode,
  6. Duration? lockTimeout,
})

Runs action inside a transaction with automatic commit on success and rollback on any failure (returned Failure or thrown exception).

Sprint 4.4 — ergonomic helper that captures the begin/commit/rollback dance behind a single call so application code never has to manage the txnId lifecycle by hand.

  • action receives the live txnId and returns a Result<T>. Returning Success(value) triggers commitTransaction; returning Failure(error) triggers rollbackTransaction and the original error is propagated.
  • When action throws, the transaction is rolled back and the exception is converted to a QueryError. The original exception is preserved in the error message for diagnostics.
  • When the rollback itself fails, the original error wins; the rollback failure is logged via the underlying repository (which already does this in rollbackTransaction).
  • Default isolation is IsolationLevel.readCommitted, default dialect is SavepointDialect.auto, default access mode is TransactionAccessMode.readWrite — same defaults as beginTransaction.

Example:

final result = await service.runInTransaction<int>(
  connId,
  (txnId) async {
    final r1 = await service.executeQueryParams(
      connId, 'INSERT INTO logs(msg) VALUES (?)', ['hi'],
    );
    if (r1.isError()) return Failure(r1.exceptionOrNull()!);
    return const Success(42);
  },
  accessMode: TransactionAccessMode.readWrite,
);

Implementation

Future<Result<T>> runInTransaction<T extends Object>(
  String connectionId,
  Future<Result<T>> Function(int txnId) action, {
  IsolationLevel? isolationLevel,
  SavepointDialect? savepointDialect,
  TransactionAccessMode? accessMode,
  Duration? lockTimeout,
});