Sqlx Acquire causes issue with axum handler when using transactions. #4319
-
|
Whenever I try to call a function using Acquire in my axum handler and I am try to use a transaction, the axum handler starts giving me error. But if I pass a PgPool to it my handler works fine. Is there anyway to fix this? The axum handler error is not helpful and using the |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
|
The trait error comes from Grab a connection inside the fn and run against that: async fn create_docs<'c, A>(conn: A) -> Result<(), sqlx::Error>
where
A: Acquire<'c, Database = Postgres> + Send,
{
let mut conn = conn.acquire().await?;
sqlx::query("...").execute(&mut *conn).await?;
Ok(())
}Then call |
Beta Was this translation helpful? Give feedback.
-
This was the solution. So, I have just decided to continue with |
Beta Was this translation helpful? Give feedback.
-
|
You can do this: async fn do_something_in_db(conn: &mut PgConnection) -> Result<(), sqlx::Error> {
query.execute(&mut *conn).await?;
query2.execute(&mut *conn).await?;
Ok(())
}
// From pool
let conn = &mut pool.acquire().await?;
do_something_in_db(conn).await?;
// From transaction
let mut tx = pool.begin().await?;
do_something_in_db(&mut *tx).await?;
tx.commit().await?; |
Beta Was this translation helpful? Give feedback.
This was the solution. So, I have just decided to continue with
&mut PgConnection. Hopefully, the bug is fixed with the diagnostics soon. So, we don't have to use this solution.