我使用使用柴油、r2d2和R2D2-柴油的连接池系统为我的web应用程序建立了一个API主机。我一直把这个博客帖子作为一个基础,帮助我建立起来。但是,我已经为我的数据库后端修改了切换到MySQL;我添加了必要的柴油特性,并假定它不会是一个问题。
下面是我用来设置连接池的代码(与博客文章中的代码非常相似):
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种以上的语言和几个应用程序所使用,没有问题,所以我对此表示怀疑。
发布于 2017-04-12 20:00:16
编辑:这已在最新版本的涉及到的板条箱中得到修正.
结果是r2d2-diesel库当前MySQL已损坏。。健康检查进行查询,但从不读取结果,这会扰乱用户创建的查询的MySQL命令排序。
我在r2d2-diesel存储库中创建了一个分支来解决这个问题。它不是作为一种长期解决方案而设计的,而且只适用于MySQL客户端。如果您想使用它,请在您的Cargo.toml中添加以下内容
r2d2-diesel-mysql = { git = "https://github.com/Ameobea/r2d2-diesel" }将代码库中的所有r2d2_diesel实例更改为r2d2_diesel_mysql,它应该作为插入替换。
https://stackoverflow.com/questions/43268670
复制相似问题