runTx<R> method

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,
})

Runs fn in a transaction.

Implementation

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