Error message:
thread 'actix-rt|system:0|arbiter:1' panicked at 'Error deleting task: DatabaseError(__Unknown, "database is locked")', src\handlers\tasks_handlers.rs:54:10
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Rust code:
#[delete("/lists/{list_id}")]
async fn delete_list_and_tasks(
pool: web::Data<SqlitePool>,
list_id: web::Path<i32>,
) -> HttpResponse {
use crate::db::schema::lists::dsl::{id as list_id_field, lists};
use crate::db::schema::tasks::dsl::{list_id as tasks_list_id_field, tasks};
let id = list_id.into_inner();
let conn = pool.get().expect("Failed to get DB connection from pool");
let deleted_tasks: Vec<Tasks> = tasks.filter(tasks_list_id_field.eq(id))
.load(&conn)
.expect("Error loading tasks with the same list_id");
let deleted_list: Lists = lists.find(id).get_result(&conn)
.expect("Error loading list");
conn.transaction::<_, diesel::result::Error, _>(|| {
diesel::delete(tasks.filter(tasks_list_id_field.eq(id)))
.execute(&conn)
.expect("Error deleting list");
diesel::delete(lists.filter(list_id_field.eq(id)))
.execute(&conn)
.expect("Error deleting list");
Ok(())
}).expect("Error deleting list and associated tasks");
let response = Response {
deleted_list,
deleted_tasks,
};
HttpResponse::Ok().json(response)
}
Purpose is to delete a list from table lists
and all tasks with the same list_id
in table tasks
. The problem occurs when I access tasks
table and when I edit or access something (when I execute the same request with Postman all seems good).
I tried to implement transactions and pools, and don't want to implement mutex because I need to access the database many times at the same time.
What can I do?