Accumulating connections

For the longest time, whenever I'm working (in dev) and my app restarts, more often times than not, I will get a connection error because I have achieved the max number of concurrent connections for my PostgreSQL DB (5). I have that limit because I use a free provider for the dev database. The only way to keep working, is to login to the DB's admin panel and kill all the connections. Problem here is, why the heck is it accumulating connections? It seems to happen randomly, I cannot track exactly when it doesn't close the connections, so I don't know what causes it. I might simply be editing a Handlebars view, which requires server restart, and once it does, there's a chance I've reached the number of connections. WTF is going on?
No description
6 Replies
Darren
Darren3w ago
I think it's to do with hot reloading. Either set the idle timeout to be really short or set a global.drizzle then only set if its undefined otherwise use the one from global. Hope that makes sense ofc only do this when node_env = development
pato
pato3w ago
Hey! Would you mind explaining how HMR might be affecting this? I’m not following
Darren
Darren3w ago
i think each time a module is reloaded it would generate a new instance of the drizzleclient, and the old one doesnt close the connection properly, and since there is no idle timeout set it stays open indefinitely in postgres, that is my understanding of it anyway
Angelelz
Angelelz3w ago
The way people solve this type of problems with HMR is by creating a singleton drizzle instance attached to globalThis. I think that if you search for singleton or HMR in this server you’ll find several answers
rphlmr ⚡
rphlmr ⚡3w ago
You can look at https://www.npmjs.com/package/@epic-web/remember. It's the same thing as globalThis
pato
pato6d ago
thank you for all the replies, I only had time to look into this now I just tried this and spent some time testing around, so far so good. I'll awake this post again if I did something wrong
Want results from more Discord servers?
Add your server