Single SQLx Postgres pool connection OR multiple pool connections? #1979
-
Should I use just one use anyhow::{Context, Result};
use sqlx::{postgres::PgPoolOptions, Pool, Postgres};
use std::sync::Arc;
//struct DatabaseConnectionPool(Arc<Pool<Postgres>>);
pub async fn run_database(db_url: String) -> Result<Arc<Pool<Postgres>>> {
// create a PgPoolOptions connection to DATABASE_URL
let db_pool_options = PgPoolOptions::new()
.max_connections(50)
.connect(&db_url.as_str())
.await
.with_context(|| "Connection to Database failed")?;
// Correctly embed into this application database migrations
// ...performed by command `$ sqlx db setup`
// ...You can use this command each time you add to dir migrations
sqlx::migrate!()
.run(&db_pool_options)
.await
.with_context(|| "SQLx DB migration failed")?;
Ok(Arc::new(db_pool_options))
} Or create multiple Both approaches work, but I really want to follow the one that is of best practice. Thanks in advance. |
Beta Was this translation helpful? Give feedback.
Answered by
davidpdrsn
May 2, 2023
Replies: 1 comment 2 replies
-
Create one PgPool and share that. |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
iodapson
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Create one PgPool and share that.