Best practice for long-running pgxpool #1700
-
In a package that is part of a web service running 24/7 I plan to:
Is this a good practice and efficient way to use Minimal example for additional clarity:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
From a pgx point of view what you are proposing will work. I usually have a single *pgxpool.Pool for my web apps. However, I prefer the pool creation to be directly called rather than rely on package init(). That makes it easier to get the database connection string from a CLI param or some other place than an envvar. In addition, I have found it helpful (albeit seemingly inconvenient at first) to pass the pool around as needed rather than use a global. This allows me to run tests that each have their own database. |
Beta Was this translation helpful? Give feedback.
From a pgx point of view what you are proposing will work. I usually have a single *pgxpool.Pool for my web apps.
However, I prefer the pool creation to be directly called rather than rely on package init(). That makes it easier to get the database connection string from a CLI param or some other place than an envvar.
In addition, I have found it helpful (albeit seemingly inconvenient at first) to pass the pool around as needed rather than use a global. This allows me to run tests that each have their own database.