Next.js Discord

Discord Forum

Databse

Answered
Siamese posted this in #help-forum
Open in Discord
SiameseOP
How can I create a persistent mongoose connection while using app router? I don't want to keep on reconnect

27 Replies

SiameseOP
Bump
Answer
thats so inefficient
and i think that once the post ends, the connection gets stopped
@Siamese they connect on every request
I think he is caching the db here
He has clearly explained it
I didn't read too much
but he explained pretty good IG
thats exactly whats happening
also if you're deploying to lets say vercel (serverless)
and you have a bunch of route handlers
each one of those will create a new connection
Oh
the first time they run
since they are all their own lambdas
I didn't read the code fully, just assumed it would work, sorry OP
connecting and disconnecting on every request when working in serverless env can be considered by design
SiameseOP
im not running serverless
the same code will work for you
memoize the connection
SiameseOP
i prefer to keep the connection cause its more performant than keep on reconnecting
Aight thanks
So, the problem is solved?
SiameseOP
i guess so
I was saying to mark linesofcode message as answer but thanks IG, I just searched on google and pasted the 2nd link, I read it tho