Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQLite backend does not close database #272

Closed
xSke opened this issue Apr 19, 2020 · 2 comments · Fixed by #281
Closed

SQLite backend does not close database #272

xSke opened this issue Apr 19, 2020 · 2 comments · Fixed by #281
Labels
bug question Further information is requested

Comments

@xSke
Copy link

xSke commented Apr 19, 2020

I'm playing around with sqlx for sqlite databases, and I'm running into issues with it not properly closing connections even when calling Pool::close() and dropping all connection handles. It leaves db-wal and db-shm temporary files in the working directory, and I end up with errors about the table being locked when attempting to load a database file again that's been improperly closed.

Here's a basic example that opens a database, executes a few queries, then exits - there'll be various temporary files left over once the program ends:

use sqlx::sqlite::SqlitePool;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let pool = SqlitePool::new("sqlite://test.db").await?;
    sqlx::query("create table if not exists test (foo text);").execute(&pool).await?;
    sqlx::query("insert into test (foo) values (?)")
        .bind("bar")
        .execute(&pool).await?;

    pool.close().await;

    Ok(())
}

I'm using sqlx 0.3.4 with features ["runtime-tokio", "macros", "sqlite"].

Do let me know if I'm just missing something obvious, but it doesn't seem so.

@mehcode
Copy link
Member

mehcode commented Apr 22, 2020

Sorry that this his happening. Can you reproduce this without using the pool? To try and narrow this down. You can connect directly using SqliteConnection::connect.

@mehcode mehcode added bug question Further information is requested labels Apr 22, 2020
@hasali19
Copy link
Contributor

I'm getting the same issue with this code:

#[tokio::main]
async fn main() -> sqlx::Result<()> {
    let mut conn = sqlx::SqliteConnection::connect("sqlite://test.db").await?;

    sqlx::query("create table if not exists test (foo text);")
        .execute(&mut conn)
        .await?;

    sqlx::query("insert into test (foo) values (?)")
        .bind("bar")
        .execute(&mut conn)
        .await?;

    Ok(())
}

After some investigating, I think that self.statement also needs to be dropped here, otherwise the connection is being closed before all statements have been finalized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants