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

can't send PooledConnection reference #2232

Closed
kanekv opened this issue Nov 28, 2019 · 3 comments
Closed

can't send PooledConnection reference #2232

kanekv opened this issue Nov 28, 2019 · 3 comments

Comments

@kanekv
Copy link

kanekv commented Nov 28, 2019

I'm trying to wrap PooledConnection in the Mutex and getting this:

`std::ptr::NonNull<pq_sys::pg_conn>` cannot be shared between threads safely
within `r2d2::PooledConnection<embedded_migrations::diesel::r2d2::ConnectionManager<embedded_migrations::diesel::PgConnection>>`, the trait `std::marker::Sync` is not implemented for `std::ptr::NonNull<pq_sys::pg_conn>`
required because it appears within the type `embedded_migrations::diesel::pg::connection::raw::RawConnection`
required because it appears within the type `embedded_migrations::diesel::PgConnection`
required because it appears within the type `r2d2::Conn<embedded_migrations::diesel::PgConnection>`
required because it appears within the type `std::option::Option<r2d2::Conn<embedded_migrations::diesel::PgConnection>>`
required because it appears within the type `r2d2::PooledConnection<embedded_migrations::diesel::r2d2::ConnectionManager<embedded_migrations::diesel::PgConnection>>`
required because of the requirements on the impl of `std::marker::Send` for `&r2d2::PooledConnection<embedded_migrations::diesel::r2d2::ConnectionManager<embedded_migrations::diesel::PgConnection>>`
required because of the requirements on the impl of `std::marker::Send` for `std::sync::Mutex<&r2d2::PooledConnection<embedded_migrations::diesel::r2d2::ConnectionManager<embedded_migrations::diesel::PgConnection>>>`

Apparently I can't pass reference to a connection?

@weiznich
Copy link
Member

That's by "design". The underlying connection is just not threadsafe, so it cannot be shared between threads.

@lamafab
Copy link

lamafab commented Jan 31, 2020

For anyone else running into this: what you probably want to do is to share a Pool instead of PooledConnection, and then call get() from within the thread.

Example 1

async fn execute(pool: &Pool<ConnectionManager<PgConnection>>, data: SomeData) -> Result<(), Error> {
    diesel::insert_into(some_table)
        .values(data)
        .execute(&pool.get()?)?;

    Ok(())
}

Example 2

pub struct Database {
    pool: Pool<ConnectionManager<PgConnection>>,
}

impl Database {
    async fn insert_some_data(&self, data: SomeData) -> Result<(), Error> {
        diesel::insert_into(some_table)
            .values(data)
            .execute(&self.pool.get()?)?;

        Ok(())
    }
}

See r2d2 docs for more: https://docs.diesel.rs/r2d2/

@ugifractal
Copy link

For anyone else running into this: what you probably want to do is to share a Pool instead of PooledConnection, and then call get() from within the thread.

Example 1

async fn execute(pool: &Pool<ConnectionManager<PgConnection>>, data: SomeData) -> Result<(), Error> {
    diesel::insert_into(some_table)
        .values(data)
        .execute(&pool.get()?)?;

    Ok(())
}

Example 2

pub struct Database {
    pool: Pool<ConnectionManager<PgConnection>>,
}

impl Database {
    async fn insert_some_data(&self, data: SomeData) -> Result<(), Error> {
        diesel::insert_into(some_table)
            .values(data)
            .execute(&self.conn.get()?)?;

        Ok(())
    }
}

See r2d2 docs for more: https://docs.diesel.rs/r2d2/

thanks for the &pool.get()

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

No branches or pull requests

4 participants