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
I have this code where I deliberately fail a select query. I can see it goes to the Err, the rollback call comes back without any error, and the println are visible. But when I check my db I can see that a new profile object was created. Shouldn't a rollback roll back everything inside the transaction?
#[actix_web::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let conn = sqlx::postgres::PgPool::connect("postgres://tester:tester@localhost:5432/tester")
.await
.unwrap();
let tx = conn.begin().await.unwrap();
let insert_result = sqlx::query_as::<_, EntityId>("insert into profile (user_name, full_name) values ($1, $2) returning id")
.bind(Username().fake::<String>())
.bind(format!("{} {}", FirstName().fake::<String>(), LastName().fake::<String>()))
.fetch_one(&conn)
.await;
let query_result = sqlx::query_as::<_, Profile>("select * from profile where id = $1")
.bind(24324)
.fetch_one(&conn)
.await;
match query_result {
Ok(profile) => {
println!("Profile: {:?}", profile);
_ = tx.commit().await;
},
Err(_) => {
println!("Error: profile {} not found", 24324);
let rollback_result = tx.rollback().await?;
println!("Rollback result: {:?}", rollback_result);
}
};
Ok(())
}
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 have this code where I deliberately fail a select query. I can see it goes to the Err, the rollback call comes back without any error, and the println are visible. But when I check my db I can see that a new profile object was created. Shouldn't a rollback roll back everything inside the transaction?
Beta Was this translation helpful? Give feedback.
All reactions