I've been using once_cell
to do a lot of work that only needs to be done once, and then persist as read-only global. This is nice because I don't have to pass these things around. I was wanting to know if something like this is permitted for db handles/pools?
static POOL: Pool<Postgres> = PgPoolOptions::new()
.max_connections(5)
.connect("postgres://postgres:password@localhost/test")
.await
.unwrap();
But alas, this doesn't work because of the .await
,
error[E0744]: `.await` is not allowed in a `static`
--> src/main.rs:10:31
And, if I try to wrap in once_cell
, I get
static POOL = Lazy::new(|| sqlx_rt::block_on(
PgPoolOptions::new()
.max_connections(5)
.connect("postgres://postgres:password@localhost/test")
.await
) );
Is there anyway to do what I want here