Search code examples
next.jsprismavercel

Prisma on production - User 'xxx' has exceeded the 'max_user_connections' resource


I'm having an issue when I upload my Next.js app in production (in Vercel). When I run the app in development, it works fine. Only 1 connection is created. But when I use the app in production, instantly reaches the max users connections (5).

I don't know what to share exactly, but this is my client.js:

import { PrismaClient } from "./generators/app";

export const prisma =
  global.prisma ||
  new PrismaClient();

if (process.env.NODE_ENV !== 'production') global.prisma = prisma;

And this is how I import it from api files:

import { prisma } from "../../../prisma/client";

I've tried both guides: https://www.prisma.io/docs/guides/performance-and-optimization/connection-management https://www.prisma.io/docs/guides/database/troubleshooting-orm/help-articles/nextjs-prisma-client-dev-practices

This is the error:

2022-10-27T20:58:32.492Z    07594f4a-727a-4e98-a4aa-264d060dc08b ERROR  PrismaClientInitializationError: 
Invalid `prisma.user.findUnique()` invocation:
Error querying the database: Server error: `ERROR 42000 (1226): User 'xxx' has exceeded the 'max_user_connections' resource (current value: 5)'
    at RequestHandler.handleRequestError (/var/task/.next/server/chunks/7738.js:31215:19)
    at RequestHandler.request (/var/task/.next/server/chunks/7738.js:31188:18)
    at async PrismaClient._request (/var/task/.next/server/chunks/7738.js:32176:24)
    at async havePermission (/var/task/.next/server/chunks/3433.js:49:40)
    at async getServerData (/var/task/.next/server/pages/api/app/user/get/[column].js:181:34)
    at async Object.apiResolver (/var/task/node_modules/next/dist/server/api-utils/node.js:366:9)
    at async NextNodeServer.runApi (/var/task/node_modules/next/dist/server/next-server.js:469:9)
    at async Object.fn (/var/task/node_modules/next/dist/server/next-server.js:719:37)
    at async Router.execute (/var/task/node_modules/next/dist/server/router.js:247:36)
    at async NextNodeServer.run (/var/task/node_modules/next/dist/server/base-server.js:346:29) {
  clientVersion: '4.3.1',
  errorCode: undefined
}
2022-10-27T20:58:32.501Z    07594f4a-727a-4e98-a4aa-260d060dc08b    ERROR   PrismaClientInitializationError: 
Invalid `prisma.user.findUnique()` invocation:
Error querying the database: Server error: `ERROR 42000 (1226): User 'xxx' has exceeded the 'max_user_connections' resource (current value: 5)'
    at RequestHandler.handleRequestError (/var/task/.next/server/chunks/7738.js:31215:19)
    at RequestHandler.request (/var/task/.next/server/chunks/7738.js:31188:18)
    at async PrismaClient._request (/var/task/.next/server/chunks/7738.js:32176:24)
    at async havePermission (/var/task/.next/server/chunks/3433.js:49:40)
    at async getServerData (/var/task/.next/server/pages/api/app/user/get/[column].js:181:34)
    at async Object.apiResolver (/var/task/node_modules/next/dist/server/api-utils/node.js:366:9)
    at async NextNodeServer.runApi (/var/task/node_modules/next/dist/server/next-server.js:469:9)
    at async Object.fn (/var/task/node_modules/next/dist/server/next-server.js:719:37)
    at async Router.execute (/var/task/node_modules/next/dist/server/router.js:247:36)
    at async NextNodeServer.run (/var/task/node_modules/next/dist/server/base-server.js:346:29) {
  clientVersion: '4.3.1',
  errorCode: undefined
}
RequestId: 07594f4a-727a-4e98-a4aa-264d060dc08b Error: Runtime exited with error: exit status 1
Runtime.ExitError

After that it tries every second to make a GET request to the api with 500 response.


Solution

  • Vercel by default creates like 5 new connections to the database (maybe for the CDNs or something like that) and you need a larger connection limit.