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

mysql: close last_stmt for non-cacheable statements #28

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/mysql/mod.rs
Expand Up @@ -22,11 +22,15 @@ use self::row::MysqlRow;
use self::serialize::ToSqlHelper;

/// A connection to a MySQL database. Connection URLs should be in the form
/// `mysql://[user[:password]@]host/database_name`
/// `mysql://[user[:password]@]host/database_name`.
///
/// See [`mysql_async::Opts`] for more available connection url queries. Notice that
/// param `stmt_cache_size` will be ignored.
pub struct AsyncMysqlConnection {
conn: mysql_async::Conn,
stmt_cache: StmtCache<Mysql, Statement>,
transaction_manager: AnsiTransactionManager,
// a workaround so that for non-cacheable stmts we still get a `&'conn Statement`.
last_stmt: Option<Statement>,
}

Expand Down Expand Up @@ -212,11 +216,14 @@ impl AsyncMysqlConnection {

let stmt = stmt_cache.cached_prepared_statement(query, &metadata, conn, &Mysql);

stmt.and_then(|(stmt, conn)|async move {

stmt.and_then(|(stmt, conn)| async move {
let stmt = match stmt {
MaybeCached::CannotCache(stmt) => {
if let Some(last_stmt) = std::mem::take(last_stmt) {
conn.close(last_stmt).await.map_err(ErrorHelper)?;
}
*last_stmt = Some(stmt);
// SAFETY: we just set last_stmt to Some.
last_stmt.as_ref().unwrap()
}
MaybeCached::Cached(s) => s,
Expand Down