0

I am currently learning Rust and Rocket

Using Rust 1.54.0+Rocket 0.5.0_rc1+ Diesel 1.4.7 + r2d2 0.8.9

I created a DB Postgres connection pool with r2d2. I want to share the connection pool between requests/Routes, to do that I am trying to use Rocket Managed State. https://rocket.rs/v0.5-rc/guide/state/#managed-state

I created a DB Connection Pool, saved it on the state, but when I tried to access that DB Connection Pool from the Route. I am getting 2 error on the same line

Cell<i32> cannot be shared between threads safely

RefCell<HashMap<StatementCacheKey<Pg>, pg::connection::stmt::Statement>> cannot be shared between threads safely

here my code

pub async fn establish_pooled_connection() -> Result<PooledConnection<ConnectionManager<PgConnection>>, r2d2_diesel::Error> {
    dotenv().ok();
    let database_url = env::var("DATABASE_URL")
        .expect("DATABASE_URL must be set");
    let manager = ConnectionManager::<PgConnection>::new(&database_url);
    let pool = r2d2::Pool::builder().build(manager).expect("Failed to create pool.");
    let conn = pool.clone().get().unwrap();
    Ok(conn)
}

struct DBPool{
    db_pool: PooledConnection<ConnectionManager<PgConnection>>
}

#[rocket::main]
async fn main() {
  
    let pool = establish_pooled_connection();    
    rocket::build()
        .mount("/",routes![callapi])
        .manage(DBPool{db_pool: pool})
        .launch()
        .await.ok();       
}

#[post("/callapi", data = "<request>")]
async fn callapi(request: RequestAPI<'_>, _dbpool: &State<DBPool>) -> Json<models::api_response::ApiResponse> {
......

The errors are for this parameter

_dbpool: &State<DBPool>

thanks in advance

4

1 回答 1

2

最后,我能够完成这项工作。

我主要将此 GitHub 存储库用作基础 https://github.com/practical-rust-web-development/mystore/tree/v1.1 该存储库使用 Actix,但我使用的是 Rocket。

我认为我主要的误解是基本的 PostgreSQL 连接是一个 PGConnection,而你从 Pool 中得到的是一个 PGPoledConnection,它最后是相同的。

这是我的代码

db_connection.rs

use diesel::pg::PgConnection;
use dotenv::dotenv;
use std::env;
use diesel::r2d2::{ Pool, PooledConnection, ConnectionManager, PoolError };

pub type PgPool = Pool<ConnectionManager<PgConnection>>;
pub type PgPooledConnection = PooledConnection<ConnectionManager<PgConnection>>;

//Connects to Postgres and call init pool
pub fn establish_connection() -> PgPool {
    dotenv().ok();

    let database_url = env::var("DATABASE_URL")
        .expect("DATABASE_URL must be set");
    init_pool(&database_url).expect("Failed to create pool")
}


//Creates a default R2D2 Postgres DB Pool
fn init_pool(database_url: &str) -> Result<PgPool, PoolError> {
    let manager = ConnectionManager::<PgConnection>::new(database_url);
    Pool::builder().build(manager)
}


//this functions returns a connection from the Pool
pub fn pg_pool_handler(pool: &PgPool) -> Result<PgPooledConnection, PoolError> {
    let _pool = pool.get().unwrap();
    Ok(_pool)
}

main.rs

mod db_connection;
use db_connection::{PgPool};

#[rocket::main]
async fn main() {
    rocket::build()
        .mount("/API",routes![demo])
        .manage(db_connection::establish_connection()) //here is where you pass the pool to Rocket state.
        .launch()
        .await.ok();       
}

#[post("/demo", data = "<request>")]
async fn demo _dbpool: &State<PgPool>) -> Json<models::Response> {

let connection = db_connection::pg_pool_handler(_dbpool).unwrap();

let results = users.limit(1)
        .load::<User>(&connection)
        .expect("Error loading users");
........

那是基本代码,可以改进代码以更好地处理错误。

于 2021-09-06T21:26:56.743 回答