steffengy / tiberius

TDS 7.4 (mssql / Microsoft SQL Server) async driver for rust. Fork at: https://github.com/prisma/tiberius
Apache License 2.0
150 stars 2 forks source link

Cannot implement a connection pool using r2d2 #81

Closed brokenthorn closed 5 years ago

brokenthorn commented 5 years ago

I commented, in #49 which is closed, because I had a similar issue, but I thought it might not get caught up there, so I'm opening a new issue with that same comment.

I'm implementing r2d2::ManageConnection. One of its fn's, is_valid, borrows the connection, mutably. The purpose of the function is to check if the connection is still... valid. The way I want to do this is to query the SQL Server for the result of "SELECT 1" like it is suggested to do. The problem is that tiberius' SqlConnection consumes self when calling query, which inside is_valid can't happen, because it's already borrowed in this context. Any suggestions?

fn is_valid(&self, conn: &mut Self::Connection) -> Result<(), Self::Error> {
    let sql_str = "SELECT 1";
    let stmt = conn.prepare(sql_str);
    let future = conn
        .query(stmt, &[])
        .for_each(move |x| {
            let result: i32 = x.get(0);
            // ...
            Ok(())
        });
    // now run the future and unwrap it
    match future.wait() {
        Ok(_) => { Ok(()) }
        Err(tiberius_error) => Err(
            MSSQLConnectionManagerError(Errors::WrappedTiberiusError(tiberius_error))
        ),
    }
}
error[E0507]: cannot move out of borrowed content
   --> src/persistence/mssql.rs:112:22
    |
112 |         let future = conn
    |                      ^^^^ cannot move out of borrowed content

error: aborting due to previous error

For more information about this error, try `rustc --explain E0507`.
steffengy commented 5 years ago

r2d2 is focused/designed for a synchronous API, so this might be quite hard and only one of the easier issues.

brokenthorn commented 5 years ago

Ah! Now it makes sense! Thank you!