Bird
0
0

Which event should you use to close database connections when a FastAPI app stops?

easy🧠 Conceptual Q2 of 15
FastAPI - Middleware and Hooks
Which event should you use to close database connections when a FastAPI app stops?
Abackground
Bstartup
Crequest
Dshutdown
Step-by-Step Solution
Solution:
  1. Step 1: Identify event for cleanup

    Shutdown events are designed to run cleanup code like closing database connections.
  2. Step 2: Exclude other events

    Startup runs at app start, request runs per HTTP request, background is unrelated to app lifecycle.
  3. Final Answer:

    shutdown -> Option D
  4. Quick Check:

    Shutdown event = Cleanup tasks [OK]
Quick Trick: Use shutdown event to clean resources before app stops [OK]
Common Mistakes:
MISTAKES
  • Using startup event for cleanup
  • Confusing request event with lifecycle events
  • Not closing resources causing leaks

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes