runInTransaction<T extends Object> abstract method
Future<Result<T> >
runInTransaction<T extends Object>(
- String connectionId,
- Future<
Result< action(T> >- int txnId
- IsolationLevel? isolationLevel,
- SavepointDialect? savepointDialect,
- TransactionAccessMode? accessMode,
- 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.
actionreceives the livetxnIdand returns aResult<T>. ReturningSuccess(value)triggerscommitTransaction; returningFailure(error)triggersrollbackTransactionand the original error is propagated.- When
actionthrows, the transaction is rolled back and the exception is converted to aQueryError. 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 isSavepointDialect.auto, default access mode isTransactionAccessMode.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,
});