Instantiating Prisma Client
The following example demonstrates how to import and instantiate your generated client from the default path:
- TypeScript
- JavaScript
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
const { PrismaClient } = require('@prisma/client')
const prisma = new PrismaClient()
You can further customize PrismaClient
with constructor parameters — for example, set logging levels, transaction options or customize error formatting.
The number of PrismaClient
instances matters
Your application should generally only create one instance of PrismaClient
. How to achieve this depends on whether you are using Prisma ORM in a long-running application or in a serverless environment .
The reason for this is that each instance of PrismaClient
manages a connection pool, which means that a large number of clients can exhaust the database connection limit. This applies to all database connectors.
If you use the MongoDB connector, connections are managed by the MongoDB driver connection pool. If you use a relational database connector, connections are managed by Prisma ORM's connection pool. Each instance of PrismaClient
creates its own pool.
-
Each client creates its own instance of the query engine.
-
Each query engine creates a connection pool with a default pool size of:
num_physical_cpus * 2 + 1
for relational databases100
for MongoDB
-
Too many connections may start to slow down your database and eventually lead to errors such as:
Error in connector: Error querying the database: db error: FATAL: sorry, too many clients already
at PrismaClientFetcher.request