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

fix(mysql): Close prepared statement if persistence is disabled #2905

Merged
merged 2 commits into from
Jan 21, 2024

Conversation

larsschumacher
Copy link
Contributor

This pull request fixes an issue that prepared statements are not closed if persistence or statement cache are disabled.

How to reproduce this issue

use sqlx::{Connection, MySqlConnection, Row};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let url = std::env::var("DATABASE_URL")?;
    let mut connection = MySqlConnection::connect(&url).await?;

    for i in 0..10 {
        let _ = sqlx::query("SELECT ? AS val")
            .bind(i)
            .persistent(false)
            .fetch_one(&mut connection)
            .await?;
    }

    let rows = sqlx::query(
        r#"
        SELECT 
              psi.STATEMENT_ID
            , psi.SQL_TEXT
        FROM performance_schema.threads AS t
        INNER JOIN performance_schema.prepared_statements_instances AS psi
            ON psi.OWNER_THREAD_ID = t.THREAD_ID 
        WHERE t.processlist_id = CONNECTION_ID()
        "#,
    )
    .fetch_all(&mut connection)
    .await?;

    for row in rows {
        let id: u64 = row.try_get("STATEMENT_ID")?;
        let sql: String = row.try_get("SQL_TEXT")?;

        println!("ID: {id}; SQL: {sql}");
    }

    Ok(())
}

Output:

ID: 1; SQL: SELECT ? AS val
ID: 3; SQL: SELECT ? AS val
ID: 5; SQL: SELECT ? AS val
ID: 7; SQL: SELECT ? AS val
ID: 9; SQL: SELECT ? AS val
ID: 11; SQL: SELECT ? AS val
ID: 13; SQL: SELECT ? AS val
ID: 15; SQL: SELECT ? AS val
ID: 17; SQL: SELECT ? AS val
ID: 19; SQL: SELECT ? AS val
ID: 21; SQL: SELECT
              psi.STATEMENT_ID
            , psi.SQL_TEXT
        FROM performance_schema.threads AS t
        INNER JOIN performance_schema.prepared_statements_instances AS psi
            ON psi.OWNER_THREAD_ID = t.THREAD_ID
        WHERE t.processlist_id = CONNECTION_ID()

I only expect the last statement in the output.

Info

SQLx version: 0.7.3
Database server: MySQL 8.0.33

@abonander abonander merged commit 29dcd44 into launchbadge:main Jan 21, 2024
64 checks passed
sticnarf pushed a commit to sticnarf/sqlx that referenced this pull request Feb 21, 2024
…chbadge#2905)

* close prepared statement if persistence or statement cache are disabled

* add tests
sticnarf pushed a commit to sticnarf/sqlx that referenced this pull request Feb 21, 2024
…chbadge#2905)

* close prepared statement if persistence or statement cache are disabled

* add tests
@sticnarf
Copy link

Hi @abonander, is there a release plan in the near future? It'll be good to get a released sqlx version with the issue fixed. Thanks.

kukabi pushed a commit to helikon-labs/sqlx that referenced this pull request Feb 22, 2024
…chbadge#2905)

* close prepared statement if persistence or statement cache are disabled

* add tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants