查询数据库时出错使用diesel,r2d2和r2d2-diesel的连接池系统设置了一个系统,作为我的Web应用程序的API主机。我一直在关注这个blog post作为帮助我解决问题的基础。但是,我已经对我的数据库后端进行了MySQL切换修改;我已经添加了必要的柴油机功能,并认为这不是问题。使用Diesel,r2d2和r2d2柴油
下面是我用它来建立一个连接池(大同小异从博客文章)的代码:
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 to create pool.")
}
建立数据库接口的过程中,我已经遇到一个问题系统。当我通过柴油对数据库进行任何查询时,出现以下错误:Err(DatabaseError(__Unknown, "Commands out of sync; you can\'t run this command now"))
我已经做了一些研究,看起来这个错误发生在先前的查询没有被读取之前发送另一个,导致我相信这可能是一个图书馆错误。我检查了MySQL查询日志,并且除了在连接池中创建连接之外,我看不到任何查询。
我把我的错误减少到测试用例。与以下错误消息响应上面我贴:通过运行类似下面的查询产生
/// Make sure we can run basic queries on the database using a 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);
}
相同的错误消息,但是这更难以降低到一个测试案例:
let query = diesel::insert(&beatmap).into(schema::beatmaps::dsl::beatmaps);
// println!("{:?}", query);
print_sql!(query);
let conn: &MysqlConnection = &*client.pool.get().expect("Unable to get connection from pool");
let res = query.execute(conn);
我想认为这是我的一个实现错误,但这有可能与我的数据库配置有关吗?我用于开发的数据库被3种语言和几个应用程序主动使用,没有问题,所以我对此表示怀疑。
这确实是柴油库和r2d2柴油的问题:https://github.com/diesel-rs/diesel/issues/728 – Ameo