warn(prisma-client) Already 10 Prisma Clients are actively running. #4399
-
I am getting this warning in my console. Can someone explain why am I getting this warning.
|
Beta Was this translation helpful? Give feedback.
Replies: 16 comments 24 replies
-
Hey @prantadutta, the issue is likely that you are creating a lot of Without knowing more about your project, it's very difficult to help you with this but I can make a few assumptions and give some hints:
It would be great if you could share a bit more info about your project 🙂 |
Beta Was this translation helpful? Give feedback.
-
Hello @prantadutta, try creating creating one prisma client and export it to the whole project, and you would still get the warning but it will exist only in development mode because next.js hard refresh just in case you don't want the warning in development mode check this link__ |
Beta Was this translation helpful? Give feedback.
-
Hi @nikolasburk, I'm getting this warning too but only in a production environment. It isn't happening locally at all even with hot-reload. I have the Prisma client in a singleton and have added logging to the singleton to log every time a new instance is created. Only one instance has been created (as expected) but I'm still getting |
Beta Was this translation helpful? Give feedback.
-
in nestjs I have to extend from prismaclient, class PrismaService extends PrismaClient and in the tests, even mocking the methods, I'm receiving this message.. how do i solve?
is there any way to tell the prism to disconnect its instance, something that stops afterEach? |
Beta Was this translation helpful? Give feedback.
-
Is there any way to silence this message? I think I might have a valid use case where this can happen expectedly by running Jest database tests in parallel with each using their own (e.g. SQLite) database - there is no problem with multiple active connections here. My tests are passing, but since Keystone updated to a more recent Prisma version, I'm being spammed by these warnings when running all tests. |
Beta Was this translation helpful? Give feedback.
-
Just to follow up. I wasn't able to reproduce this in a minimal project but I'm not seeing this message anymore after upgrading to version 3.12. I've upgraded again to v3.13 and I'm still not seeing this message. Also, this didn't cause any issues while I was seeing it. |
Beta Was this translation helpful? Give feedback.
-
Has anyone encountered an issue when Next.js build some number of static pages during the build, which require DB calls, and due to (assumed) high parallelization of these static page builds, there are multiple DB connections spawned simultaneously, tightly correlated with the number of pages being built. The solution for hot reload does not seem to help in such case, as we always had it in our codebase. Basically if we pre-build say 10 pages into static, through the build process the nextjs builder opens 10 connections to DB. The bigger problem is that these connections seem to stale, and it's unclear where to edit the code to force terminate them. |
Beta Was this translation helpful? Give feedback.
-
For those who might face this problem in nestjs, below is what i did. Although it is not that pretty, but it can squelch the warning message. If you setup your test with The code below is simple workout of making
|
Beta Was this translation helpful? Give feedback.
-
For normal application code, there are solutions like the Singleton pattern, as @nikolasburk showed us. However, there are times when you are developing Prisma extends and want a new instance of PrismaClient for each test case. In this case, the singleton pattern would share the instance with all test cases. I tried disconnecting explicitly for each test case instead of singleton. The warnings still did not go away, so I tried explicitly connecting and then explicitly disconnecting. The warnings still did not go away. test('Test Case 1', async () => {
const prisma = new PrismaClient().$extends(myextends(options));
await prisma.$connect();
// test...
await prisma.$disconnect();
});
test('Test Case 2', async () => {
const prisma = new PrismaClient().$extends(myextends(options));
await prisma.$connect();
// test...
await prisma.$disconnect();
});
... Doesn't $disconnect() improve this problem? |
Beta Was this translation helpful? Give feedback.
-
Proposed solution (singleton pattern)prisma.service.tsimport { Injectable, type OnModuleInit } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';
@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit {
private static instance: PrismaService | undefined;
public static getInstance(): PrismaService {
if (!PrismaService.instance) {
PrismaService.instance = new PrismaService();
}
return PrismaService.instance;
}
async onModuleInit(): Promise<void> {
await this.$connect();
}
} prisma.module.tsimport { Global, Module } from '@nestjs/common';
import { PrismaService } from './prisma.service';
@Global()
@Module({
providers: [
{
provide: PrismaService,
useValue: PrismaService.getInstance(),
},
],
exports: [PrismaService],
})
export class PrismaModule {} For any class or test file that injects |
Beta Was this translation helpful? Give feedback.
-
I have a multi-tenant application where I use Prisma to connect to multiple (hundreds) of databases with the same schema. I get this warning where I don't really want it, I need different connection pools. Is there a way to silence it? |
Beta Was this translation helpful? Give feedback.
-
Same for us, we have 2 use-cases that we consider valid and would appreciate being able to silence the error.
I understand the desire to warn users in the 90+% of use-cases but there are still use-cases where having many instances is valid. So having the ability to silence this error would be beneficial. |
Beta Was this translation helpful? Give feedback.
-
It declares a global variable named prisma with a type of PrismaClient | undefined. This allows the code to store the Prisma client globally, making it accessible throughout the application. Prisma Client Initialization: Global Assignment in Development: |
Beta Was this translation helpful? Give feedback.
-
I am using NextJS 14 App Router with Prisma ORMAt my project root, I have a folder called
But for some reason I am still getting this error: Can anyone tell me what is going on..? |
Beta Was this translation helpful? Give feedback.
-
From the code you provided, it looks like you're trying to create a singleton pattern for the Prisma Client using prismaClientSingleton. However, there are a few potential issues in your implementation that could be causing this problem: Conditional declaration of prismaGlobal: You're declaring prismaGlobal inside prismaClientSingleton, which means it's only accessible within that function scope. Instead, you should declare it globally so that it's accessible throughout your application. Exporting the Prisma Client instance directly: You're exporting the Prisma Client instance directly from prisma.ts, which could lead to multiple instances being created if the file is imported multiple times.
|
Beta Was this translation helpful? Give feedback.
-
This above warning indicates that you're initializing Prisma Client multiple times in your application.
Let's take a simple Example of a Next.js 14 Application:/app/utility/prisma.js
/app/utility/userFunctions.js
/app/page.tsx
|
Beta Was this translation helpful? Give feedback.
I think you could add a file
_base.js
(for example) within your folderapi
, create thePrismaClient
there and import it with your api routes.