run<R> method

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

Runs fn outside of a transaction.

Implementation

Future<R> run<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 _withConnection(
          (c) => fn(_PgExecutionContextWrapper(
            c.connectionId,
            c.connection,
            sessionId,
            traceId,
            _events,
          )),
        );
      },
      retryIf: (e) async =>
          e is! PostgreSQLException &&
          e is! IOException &&
          (retryIf == null || await retryIf(e)),
    );
  } catch (e) {
    if (orElse != null) {
      return await orElse();
    }
    rethrow;
  }
}