N
Neon15mo ago
optimistic-gold

@harshgoyal3823 , If you are using free

@harshgoyal3823 , If you are using free tier of NeonDB then it downscales itself in five minutes, That's why it might not be working for you, Consider it like that if you are not using your database for five minutes then your machine gets shutdown and you again have to connect to database
33 Replies
equal-aqua
equal-aqua15mo ago
@Not-Qualified But I am connecting to database in every request that I am making
manual-pink
manual-pink15mo ago
Can you show me your connection part ?
equal-aqua
equal-aqua15mo ago
Its at the top of the handlesignup function.
async function handleSignup(req: ExtendedApiRequest, res: NextApiResponse) {
const { name, email, password } = req.body;

// Hash password
const hashedPassword = await bcrypt.hash(password, saltRounds);
const db = neon(process.env.DATABASE_URL as string);

// more logic
async function handleSignup(req: ExtendedApiRequest, res: NextApiResponse) {
const { name, email, password } = req.body;

// Hash password
const hashedPassword = await bcrypt.hash(password, saltRounds);
const db = neon(process.env.DATABASE_URL as string);

// more logic
@Not-Qualified See that there is const db = neon(process.env.DATABASE_URL as string)
manual-pink
manual-pink15mo ago
Is neon() is custom function or something that neonDB provides with it's library ?
equal-aqua
equal-aqua15mo ago
Its provided by neon import { neon } from '@neondatabase/serverless';
manual-pink
manual-pink15mo ago
Got it Check your connection string It's correct or not That would be first step
equal-aqua
equal-aqua15mo ago
It is correct. In case of Select * from users, the connection works also
manual-pink
manual-pink15mo ago
Meaning if you put SELECT statment within db() then it's working and it's only not working for INSERT statement, Correct ? I guess, your db variable is getting confused here, Is it?
equal-aqua
equal-aqua15mo ago
Its working now for Insert also, I am literally confused, its working sometimes and sometimes it is not. I dont know what is the reason
manual-pink
manual-pink15mo ago
Good, Then For free tier, There's limit for connections and compute resources, So, mind that
equal-aqua
equal-aqua15mo ago
I would assume so, I think it is related to neon tech free tier maybe. I dont know how can I resolve it though
manual-pink
manual-pink15mo ago
SHOW max_connections;
SELECT COUNT(*) FROM pg_stat_activity;
SHOW max_connections;
SELECT COUNT(*) FROM pg_stat_activity;
equal-aqua
equal-aqua15mo ago
where to use it?
manual-pink
manual-pink15mo ago
This is as many connections that NeonDB can handle and it will tell you that how many currently are active connection to database are there Use it directly in Neon Console
equal-aqua
equal-aqua15mo ago
yeah got it, but my current connections are way less than mex allowed
manual-pink
manual-pink15mo ago
As you are saying that your queries sometimes working and sometimes don't is it happening very frequently ? Are you able to reproduce this on every other requests ?
equal-aqua
equal-aqua15mo ago
maybe that's why its working fine. Do we have any function to close the connection once the result is returned? I checked for it, couldnt find it It is happening very frequently, though for now, it seems to work fine. Maybe some compute issue only.
manual-pink
manual-pink15mo ago
If it happens again, I would suggest that you record video and post it here and tag anyone from Neon Team They will surely look into it They are very active
equal-aqua
equal-aqua15mo ago
Thanks for the help. really appreciate it. Saved me after 6hrs of frustration
manual-pink
manual-pink15mo ago
In which region your project is ?
equal-aqua
equal-aqua15mo ago
US east
manual-pink
manual-pink15mo ago
Looks like your personal project You working on sunday
equal-aqua
equal-aqua15mo ago
More like a Take home assignment, have to submit it tomorrow
manual-pink
manual-pink15mo ago
College Student ?
equal-aqua
equal-aqua15mo ago
Nope, working professional
manual-pink
manual-pink15mo ago
I'm backend engineer
equal-aqua
equal-aqua15mo ago
I am a frontend engineer
manual-pink
manual-pink15mo ago
Frontend engineer working with database ?, Sounds like you are on your journey to being the full stack guy
frozen-sapphire
frozen-sapphire15mo ago
So this isn't really related to the free tier. And I don't think it's related to connection limits. (Just make sure you're using the pooled connection string)
Its working now for Insert also, I am literally confused, its working sometimes and sometimes it is not. I dont know what is the reason
Not sure tbh why you ran into an issue specifically with inserts 🤔 Glad it's working though
SHOW max_connections;
SELECT COUNT(*) FROM pg_stat_activity;
SHOW max_connections;
SELECT COUNT(*) FROM pg_stat_activity;
This shows the number of connections when using the non-pooled connection string
equal-aqua
equal-aqua15mo ago
Ok, I was not using the pooled connection. Is there any specific reason to use pooled connection string?
frozen-sapphire
frozen-sapphire15mo ago
If you're gonna deploy your Next.js app to a serverless platform (e.g Vercel), your API endpoints become serverless functions. When querying your database from those environments, each function invocation will open a new connection to the database. This process is slow and resource-intensive. So the solution is to connect via a connection pooler. (this is managed for you by Neon and it's exposed via the pooled connection string). It maintains a pool of connections that can be reused to avoid this issue.
equal-aqua
equal-aqua15mo ago
Got it, thanks This is still persisting, I changed to pool connection string, but still getting connection timeout error
extended-yellow
extended-yellow15mo ago
Hey @harshgoyal3823, Can you share your updated code?

Did you find this page helpful?