runTx<R> method

  1. @override
Future<R> runTx<R>(
  1. PgSessionFn<R> fn, {
  2. RetryOptions? retryOptions,
  3. FutureOr<R> orElse()?,
  4. FutureOr<bool> retryIf(
    1. Exception
    )?,
  5. String? sessionId,
  6. String? traceId,
})
override

Runs fn in a transaction.

Implementation

@override
Future<R> runTx<R>(
  PgSessionFn<R> fn, {
  RetryOptions? retryOptions,
  FutureOr<R> Function()? orElse,
  FutureOr<bool> Function(Exception)? retryIf,
  String? sessionId,
  String? traceId,
}) async {
  retryOptions ??= settings.retryOptions;
  try {
    return await retryOptions.retry(
      () async {
        return await _withNode((node) async {
          return await node._pgPool.runTx(
            fn,
            retryOptions: const RetryOptions(maxAttempts: 1),
            sessionId: sessionId,
            traceId: traceId,
          );
        });
      },
      retryIf: (e) async =>
          e is! PostgreSQLException &&
          e is! IOException &&
          (retryIf == null || await retryIf(e)),
    );
  } catch (e) {
    if (orElse != null) {
      return await orElse();
    }
    rethrow;
  }
}