Skip to content

Don't consume ConnectionLike when executing queries #103

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

Closed
nicoburns opened this issue Mar 20, 2020 · 3 comments
Closed

Don't consume ConnectionLike when executing queries #103

nicoburns opened this issue Mar 20, 2020 · 3 comments
Assignees

Comments

@nicoburns
Copy link

I want to do something like the following:

async fn execute_queries(sql: String, rx: Receiver<Params>) {
    let conn = Conn::new(opts).await.unwrap();
    let stmt = conn.prepare(sql).await.unwrap();

    // Next query loop
    while let Some(params) = rx.recv().await {
        // Query retry loop
        loop {
            match stmt.execute(all_params).await {
                // Query suceeded.
                Ok(res) => break,

                // Query failed: wait 3 seconds then retry query
                Err(err @ SqlError::Driver(_)) | Err(err @ SqlError::Server(_)) => {
                    println!("Error: {}", err);
                    delay_for(Duration::from_secs(3)).await;
                    continue 'query_loop;
                }

                // Entire connection failed
                Err(err) => panic!("Placeholder for more sophisticated reconnect logic I have in actual code.")
            }
        }
    }
}

However, this is not possible because stmt.execute consumes stmt, and does not return it in the error case. In the case of success it is possible to reuse the prepared stmt, but it's awkward and involves reassigning the variable as implemented in the batch method.

Could we change execute and similar methods to take &mut self rather than self? This would make reusing connections/prepared statements possible in the case of errors, and make even the success case more ergonomic.

@blackbeam
Copy link
Owner

Hi. Yeah, this issue is already mentioned (see here) and I'm planning to fix it.

I suggest you to use an instance of Pool until then:

async fn execute_queries(
    pool: Pool,
    sql: String,
    mut rx: Receiver<Params>,
) -> super::Result<()> {
    // Next query loop
    while let Some(params) = rx.recv().await {
        // Query retry loop
        loop {
            let conn = pool.get_conn().await?;
            match conn.prep_exec(&sql, params.clone()).await {
                // Query suceeded.
                Ok(res) => break,

                // Query failed: wait 3 seconds then retry query
                Err(err @ Error::Driver(_)) | Err(err @ Error::Server(_)) => {
                    println!("Error: {}", err);
                    delay_for(Duration::from_secs(3)).await;
                    continue;
                }

                // Entire connection failed
                Err(err) => panic!("Placeholder for more sophisticated reconnect logic I have in actual code.")
            }
        }
    }

    Ok(())
}

Pool will try to return most recently used connection in get_conn, so prepared statement will be in its local cache on the second and subsequent iterations.

@blackbeam
Copy link
Owner

blackbeam commented Apr 13, 2020

@nicoburns, new non-consuming API is available in v0.24.0-alpha. Could you, please, test it? (api docs)

@nicoburns
Copy link
Author

Seems to be working for me. Good work fixing this one. I thought it was going to be months, but in fact it only took you one!

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

No branches or pull requests

2 participants