Databse
Answered
Siamese posted this in #help-forum
SiameseOP
How can I create a persistent mongoose connection while using app router? I don't want to keep on reconnect
Answered by averydelusionalperson
27 Replies
SiameseOP
Bump
Answer
@averydelusionalperson checkout this: https://medium.com/@oruchan.asar/nextjs-13-app-backend-development-d044c804c3
SiameseOP
they connect on every request
thats so inefficient
and i think that once the post ends, the connection gets stopped
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
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