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/
これに遭遇した他の人のために:あなたが_おそらく_やりたいことは、$#$
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()に感謝します
最も参考になるコメント
これに遭遇した他の人のために:あなたがおそらくやりたいことは、$#$
PooledConnection
Pool
を共有し、スレッド内からget()
を呼び出すことです。例1
例2
詳細については
r2d2
のドキュメントを参照してください: https ://docs.diesel.rs/r2d2/