Bird
0
0

How can you ensure PrismaClient disconnects properly when your NestJS app shuts down?

hard📝 Application Q9 of 15
NestJS - Database with Prisma
How can you ensure PrismaClient disconnects properly when your NestJS app shuts down?
ANo need to disconnect PrismaClient manually
BCall prisma.$disconnect() in every service method
CRely on Node.js garbage collection to close connections
DImplement OnModuleDestroy lifecycle hook and call prisma.$disconnect()
Step-by-Step Solution
Solution:
  1. Step 1: Use NestJS lifecycle hooks

    Implementing OnModuleDestroy allows cleanup when module is destroyed.
  2. Step 2: Call prisma.$disconnect()

    Calling prisma.$disconnect() closes database connections gracefully.
  3. Final Answer:

    Implement OnModuleDestroy lifecycle hook and call prisma.$disconnect() -> Option D
  4. Quick Check:

    Use OnModuleDestroy to disconnect PrismaClient properly [OK]
Quick Trick: Disconnect PrismaClient in OnModuleDestroy hook [OK]
Common Mistakes:
  • Disconnecting in every method causing overhead
  • Ignoring disconnection leading to open connections
  • Assuming garbage collection handles disconnection

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes