runTx<R> method
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;
}
}