2017-04-07 25 views
2

查询数据库时出错使用dieselr2d2r2d2-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种语言和几个应用程序主动使用,没有问题,所以我对此表示怀疑。

+0

这确实是柴油库和r2d2柴油的问题:https://github.com/diesel-rs/diesel/issues/728 – Ameo

回答

3

编辑:这已被修复在涉及的箱子的最新版本。

原来,r2d2-dieselis currently broken for MySQL。运行状况检查会进行查询,但从不读取结果,这会混淆MySQL命令,以排序用户创建的查询。

我做了一个修复问题的r2d2-diesel存储库的一个分支。它不是作为长期解决方案设计的,并且仅适用于MySQL客户端。如果你想使用它,以下内容添加到您的Cargo.toml:中r2d2_diesel

r2d2-diesel-mysql = { git = "https://github.com/Ameobea/r2d2-diesel" } 

更改所有实例r2d2_diesel_mysql在你的代码,它应该作为一个简易替换工作。

+1

修复此问题已发布。 –