Skip to content

Error "database table is locked" when using fetch_one with sqlite executor #662

Closed

Description

@snorfalorpagus

It seems the fetch_one function in the sqlite executor doesn't release the handle to the table. If the table is subsequently dropped an error is returned because of the lock on the table.

use sqlx::{Connection, Executor, Row};
use sqlx::sqlite::SqliteConnection;

#[tokio::main]
async fn main() {
    let mut conn = SqliteConnection::connect("sqlite::memory:").await.unwrap();
    conn.execute("CREATE TABLE foobar (id INTEGER)").await.unwrap();
    conn.execute("INSERT INTO foobar VALUES (42)").await.unwrap();

    let r = sqlx::query("SELECT id FROM foobar").fetch_one(&mut conn).await.unwrap();
    let x: i32 = r.try_get("id").unwrap();
    println!("{:}", x);

    conn.execute("DROP TABLE foobar").await.unwrap();  // panics!
    // thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Database(SqliteError { code: 6, message: "database table is locked" })', src\main.rs:13:5
}
[dependencies]
tokio = { version = "0.2", features = ["full"] }
sqlx = { version = "0.4.0-beta.1", default-features = false, features = [ "sqlite", "runtime-tokio", "macros" ] }
$ rustc --version
rustc 1.48.0-nightly (397b390cc 2020-08-27)

Activity

  1. songday commented on Sep 4, 2020

    @songday

    I don't know whether we met the same situation, my case is that CPU usage goes to 100%
    if I change fetch_one to fetch_all, everything is fine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions