Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Prisma · Module

$connect

$connect() explicitly establishes Prisma Client's connection to the data source. It is usually unnecessary because Prisma connects lazily on the first query, but it can prepare the connection before requests arrive.

How it works

$connect() explicitly opens Prisma Client's connection to the data source before queries run. Prisma normally connects lazily on the first request, so explicit connection is mainly useful when startup should establish readiness before handling work.

Syntax

await prisma.$connect()

Returns

A promise that fulfills when Prisma Client has connected to the data source.

Key points

  • Prisma Client exposes $connect() for opening the data-source connection and $disconnect() for closing it.
  • The client normally delays connection establishment until the first API request.
  • An explicit call opens the data-source connection before normal query traffic begins.
  • After connection setup, Prisma Client manages database connections through a pool.

Common mistakes

  • Calling $connect() is usually unnecessary because Prisma Client connects automatically when the first query is made.
  • Use explicit connection during startup when avoiding connection setup latency on the first request matters.
  • A query can establish the connection without a preceding $connect() call.

Example: Explicitly connect and disconnect

import { PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();

async function main() {
  await prisma.$connect();
  try {
    console.log(await prisma.user.findMany());
  } finally {
    await prisma.$disconnect();
  }
}

main().catch(console.error);

Connect before the first query, then disconnect when the program finishes.

When to use it

Pre-establish readiness

Application startup

Avoid initial connection delay

Latency-sensitive first request

Fail early during startup

Connection verification

Interview questions

QWhat does Prisma Client's $connect() do?

$connect() explicitly establishes Prisma Client's connection to the data source. Normally, Prisma invokes it implicitly when the first API request or query occurs.

Follow-up: Why call it explicitly? It can move connection setup to startup so the first user request does not pay the lazy-connection cost.

QIs $connect() required before every query?

It is generally optional because Prisma Client uses lazy connection behavior. The first query opens the connection automatically if the client has not already connected.

Follow-up: What lifecycle method complements it? $disconnect() closes the client and its database connections when they are no longer needed.

QWhen is explicit $connect() useful?

Call $connect() during application startup when the application needs to establish the data-source connection before serving requests. This is useful when the first request must respond immediately.

Follow-up: How should a short-lived script clean up? Use a finally block to call $disconnect() after work completes, including when the work fails.

  • $disconnect()
  • PrismaClient
  • connection pool
  • lazy connect

Source: Connection management. Checked 2026-10-16.