You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/// Here database.pool is a connection poolimplDatabase{/// Retries a transaction until it succeeds/// Note: Only errors originating from `Transaction::commit` are retried/// Errors originating from the transaction function are not retriedpubasyncfnretry_tx<'a,'b,S,F,O,E>(&self,f:S) -> Result<O,E>whereF:Future<Output = Result<O,E>> + 'b,S:Fn(&'a mut sqlx::PgConnection) -> F + Send + Sync,E:From<sqlx::Error>,'a:'b,{letmut res;letmut counter = 0;loop{letmut tx = self.pool.begin().await?;
res = f(&mut tx).await;if res.is_err(){
tx.rollback().await?;return res;}match tx.commit().await{Err(e) => {
counter += 1;if counter > TX_RETRY_LIMIT{returnErr(e.into());}}Ok(_) => break,}}
res
}
I’ve tried all sorts of lifetime shenanigans but rust seems convinced that borrowing &mut tx inside f is invalid. It feels like these lifetimes should work, but alas they do not.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I’ve run into a rather tough lifetime issue.
With the goal of building txs like this:
I’ve tried all sorts of lifetime shenanigans but rust seems convinced that borrowing &mut tx inside f is invalid. It feels like these lifetimes should work, but alas they do not.
Beta Was this translation helpful? Give feedback.
All reactions