Getting Connection closed. randomly
We have been using Vercel/Next.js/Prisma for a bit and in one specific spot we have
prisma.salesOrder.findMany() including a bunch of other tables (not ideal I know).
When we use order by in this operation, we get "Connection closed.". Things I have done so far:
- Changing the connection string from connect_timeout=15&connection_limit=20&pool_timeout=60 to connect_timeout=60&connection_limit=30&pool_timeout=0
- Decreased the QUERY_BATCH_SIZE from 1000 to 500
Even after that, I see the requests end after 13.something seconds (always less than 14s). I don't have any hard limit in Vercel with 14s, so trying to understand why the variables above are not working or what I can do. Thanks2 Replies
You selected the bot's instantaneous insights. Let me compile your answer right away!
It sounds like you're running into a connection management or timeout issue with Prisma on Vercel/Next.js, especially when using complex queries with
order by. Here are some key points and possible causes based on the knowledge sources:
1. Serverless Environment Connection Limits: On Vercel, each serverless function invocation can create its own database connection. If you set a high connection_limit, many concurrent invocations can quickly exhaust your database's connection pool, leading to instability or "connection closed" errors. The recommended starting point for serverless is connection_limit=1, then tune as needed for parallel queries, but always be careful not to exceed your DB's max connections [Prisma Docs: Serverless connection pool size].
2. Pool Timeout Behavior: Setting pool_timeout=0 disables the timeout, but in some Prisma versions, a hard-coded timeout (~30s) may still apply due to internal limitations. However, your error occurs at ~14s, which is not a standard Prisma or Vercel limit according to the docs [Prisma Docs: Pool timeout], [GitHub Issue: pool_timeout bug].
3. Query Complexity and Execution Time: Large or complex queries (especially with multiple joins and order by) can take longer to execute, increasing the risk of timeouts or connection drops, especially if the connection pool is saturated or the DB is under load [Prisma Docs: Optimizing the connection pool].
4. Vercel Function Timeout: While you mention no hard 14s limit, Vercel's documented serverless function timeout is
10s for Hobby and 60s for Pro/Enterprise, so this shouldn't be the cause unless your project is misconfigured.
Suggestions:
- Try lowering connection_limit to 1 if not using an external pooler.
- Profile the query to see if it can be optimized or split.
- Consider using an external connection pooler (like PgBouncer) if you need higher concurrency.
- Ensure you're using the latest Prisma version, as some timeout bugs have been fixed in recent releases [GitHub Issue: pool_timeout bug].
If the problem persists, it may be due to a combination of query complexity and serverless connection management. The knowledge sources do not mention a 14s-specific timeout, so this may require further investigation or a minimal reproduction for Prisma support.
🤔 Alright, thanks for letting us know that there's still some help needed. A team member will reply to you shortly.
In the meantime, please let us know any additional info, questions, or which parts of the answer may need clarification.Hey!
Can you enable tracing and see where exactly is this 14s time spent?
That should help in debugging this
OpenTelemetry tracing | Prisma Documentation
Diagnose application performance with detailed traces of each query.