Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: r2d2_sqlite connection pooling support #79

Closed
sbrl opened this issue Mar 31, 2020 · 4 comments
Closed

Feature request: r2d2_sqlite connection pooling support #79

sbrl opened this issue Mar 31, 2020 · 4 comments
Labels
enhancement New feature or request

Comments

@sbrl
Copy link

sbrl commented Mar 31, 2020

Does refinery have an option to support r2d2_sqlite for connection pooling?

The specific error I'm getting is as follows:

]error[E0277]: the trait bound `&mut r2d2::PooledConnection<r2d2_sqlite::SqliteConnectionManager>: refinery_migrations::traits::sync::Query<std::vec::Vec<refinery_migrations::AppliedMigration>>` is not satisfied
  --> wopplebloxd/src/db/migrations.rs:23:44
   |
23 |         embedded::migrations::runner().run(&mut connection);
   |                                            ^^^^^^^^^^^^^^^ the trait `refinery_migrations::traits::sync::Query<std::vec::Vec<refinery_migrations::AppliedMigration>>` is not implemented for `&mut r2d2::PooledConnection<r2d2_sqlite::SqliteConnectionManager>`
   |
   = note: required because of the requirements on the impl of `refinery::Migrate` for `&mut r2d2::PooledConnection<r2d2_sqlite::SqliteConnectionManager>`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
error: could not compile `wopplebloxd`.

To learn more, run the command again with --verbose.

I'm having to use connection pooling here because I'm setting up a web server with actix, and I'm storing my db connection in my global state object, which is access by multiple threads I assume that actix is spawning.

@jxs
Copy link
Member

jxs commented Jun 3, 2020

hi, sorry for the late reply, as I only got to check connection pools while looking into #105.
But doesn't with_init solve the problem?

@jxs
Copy link
Member

jxs commented Jul 1, 2020

i am going to close this for now

@jxs jxs closed this as completed Jul 1, 2020
@kartikynwa
Copy link

with_init is not ideal. But I have only just begun testing this so can't say for sure.

The issue is that you would want to apply refinery migrations only once when the pool is being created. with_init on the other hand will try to do this for each connection in the pool.

@kartikynwa
Copy link

kartikynwa commented Apr 2, 2024

Turns out we can take a connection for r2d2's connection pool and use with Runner::run(...). r2d2's PooledConnection implements DerefMut<Target = OriginalConnection>where OriginalConnection is whatever Connection struct PooledConnection is an abstraction over (in our case, it's rusqlite::Connection). So we can do something like for example for r2d2 and r2d2_rusqlite:

    let pool = r2d2::Pool::builder()
        .max_size(8)
        .build(manager)
        .expect("Unable to initialize SQLite connection pool");

    refinery::embed_migrations!("./migrations");
    migrations::runner()
        .run(pool.get().unwrap().deref_mut())
        .unwrap();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants