pub async fn run_in_isolated_tx<F, Fut, T>(
pool: &PgPool,
isolation: TransactionIsolation,
body: F,
) -> Result<T, CratestackError>where
F: FnMut(Transaction<'static, Postgres>) -> Fut,
Fut: Future<Output = Result<(T, Transaction<'static, Postgres>), CratestackError>>,Expand description
Begin a transaction at the requested isolation level, run body against
the live transaction, and commit. On 40001 (serialization_failure) or
40P01 (deadlock_detected) the transaction is rolled back and the body
runs again, up to MAX_RETRIES_DEFAULT times. Other errors propagate
immediately. Only a database error is retried: an error body builds
itself (Validation, Internal, …) is returned as is even when its
text contains 40001 or deadlock detected, and a typed database error
is retried by its SQLSTATE alone, never by its text.
body receives a mutable transaction reference; it should run all of
its SQL through that reference so the writes participate in the same
transaction.