Skip to content

How do you do pooling properly? #2508

Answered by Alfredoes234
Alfredoes234 asked this question in Q&A
Discussion options

You must be logged in to vote

What I changed to make it work was.

#[derive(Clone)]
struct AppState {
    pool: MySqlPool,
}



let pool = MySqlPoolOptions::new()
        .max_connections(5)
        .connect(&database_url)
        .await
        .unwrap();
    let state = AppState { pool };
    let app = Router::new()
        .route("/", get(index))
        .route("/api/user_a", post(add_user))
        .layer(middleware::map_response(main_response_mapper))
        .with_state(state);


async fn add_user(
    State(pool): State<AppState>,
    Json(params): Json<NewUser>,
) -> Json<UserBody<User>> {
    println!("{params:?}");
    let result = sqlx::query!(
        r#"
        INSERT INTO Users(name, email, password)

Replies: 3 comments 3 replies

Comment options

You must be logged in to vote
1 reply
@davidpdrsn
Comment options

Comment options

You must be logged in to vote
2 replies
@Alfredoes234
Comment options

@davidpdrsn
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by Alfredoes234
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants