runTx<R> method
Future<R>
runTx<R>(
- PgSessionFn<
R> fn, { - RetryOptions? retryOptions,
- FutureOr<
R> orElse()?, - FutureOr<
bool> retryIf()?, - String? sessionId,
- 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;
}
}