mysql - Error querying database using Diesel, r2d2, and r2d2-diesel -
i've set system using connection pooling system utilizing diesel, r2d2, , r2d2-diesel serve api host web application. i've been following blog post basis has helped me set things up. however, i've made modification of switching mysql database backend; i've added necessary diesel features , assumed wouldn't issue.
here's code use set connection pool (very same blog post):
use diesel::prelude::*; use diesel::mysql::mysqlconnection; use r2d2::{ gettimeout, pool, pooledconnection, config }; use r2d2_diesel::connectionmanager; pub struct db(pooledconnection<connectionmanager<mysqlconnection>>); impl db { pub fn conn(&self) -> &mysqlconnection { &*self.0 } } pub fn create_db_pool() -> pool<connectionmanager<mysqlconnection>> { let config = config::default(); let manager = connectionmanager::<mysqlconnection>::new(format!("{}", db_credentials)); pool::new(config, manager).expect("failed create pool.") }
i've been encountering issue during process of setting database interface system. when make query database through diesel, following error: err(databaseerror(__unknown, "commands out of sync; can\'t run command now"))
i've done research , seems error occurs when previous query hasn't been read before sending another, leading me believe may library error. checked mysql query log, , see no queries @ other creating connections in connection pools.
i've reduced error down test case. following responds error message pasted above:
/// make sure can run basic queries on database using connection pool #[test] fn basic_queries() { use diesel::connection::simpleconnection; let mut pool = create_db_pool(); let mut conn = pool.get().unwrap(); let res = conn.batch_execute("select 1"); println!("{:?}", res); }
the same error message produced running query following, that's more difficult reduce single test case:
let query = diesel::insert(&beatmap).into(schema::beatmaps::dsl::beatmaps); // println!("{:?}", query); print_sql!(query); let conn: &mysqlconnection = &*client.pool.get().expect("unable connection pool"); let res = query.execute(conn);
i'd think implementation error on part, possible has database configuration? database i'm using development in active use on 3 languages , several applications without issue, doubt it.
edit: has been fixed in latest versions of involved crates.
it turns out r2d2-diesel
library is broken mysql. health check makes query never reads result out messes mysql command ordering user-made queries.
i made fork of r2d2-diesel
repository fixes issue. not designed long-term solution , only works mysql clients. if you'd use it, add following cargo.toml
:
r2d2-diesel-mysql = { git = "https://github.com/ameobea/r2d2-diesel" }
change instances of r2d2_diesel
r2d2_diesel_mysql
in codebase , should work drop-in replacement.
Comments
Post a Comment