Diesel: PooledConnection参照を送信できません

作成日 2019年11月28日  ·  3コメント  ·  ソース: diesel-rs/diesel

PooledConnectionをミューテックスでラップしようとしています。これを取得します。

`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>>>`

どうやら私は接続への参照を渡すことができませんか?

最も参考になるコメント

これに遭遇した他の人のために:あなたがおそらくやりたいことは、$#$ PooledConnection Poolを共有し、スレッド内からget()を呼び出すことです。

例1

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

    Ok(())
}

例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(())
    }
}

詳細についてはr2d2のドキュメントを参照してください: https ://docs.diesel.rs/r2d2/

全てのコメント3件

それは「設計」によるものです。 基盤となる接続はスレッドセーフではないため、スレッド間で共有することはできません。

これに遭遇した他の人のために:あなたがおそらくやりたいことは、$#$ PooledConnection Poolを共有し、スレッド内からget()を呼び出すことです。

例1

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

    Ok(())
}

例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(())
    }
}

詳細についてはr2d2のドキュメントを参照してください: https ://docs.diesel.rs/r2d2/

これに遭遇した他の人のために:あなたが_おそらく_やりたいことは、$#$ PooledConnection Poolを共有し、スレッド内からget()を呼び出すことです。

例1

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

    Ok(())
}

例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(())
    }
}

詳細についてはr2d2のドキュメントを参照してください: https ://docs.diesel.rs/r2d2/

&pool.get()に感謝します

このページは役に立ちましたか?
0 / 5 - 0 評価