Bird
0
0

What will happen if you forget to close the database session in a FastAPI dependency after yielding it?

medium📝 component behavior Q5 of 15
FastAPI - Database Integration
What will happen if you forget to close the database session in a FastAPI dependency after yielding it?
AThe application will crash immediately
BDatabase connections may remain open, causing resource leaks
CFastAPI automatically closes the session for you
DThe session will close only after server restart
Step-by-Step Solution
Solution:
  1. Step 1: Understand session lifecycle

    Not closing sessions leaves connections open, which wastes resources and can exhaust database limits.
  2. Step 2: Evaluate other options

    FastAPI does not auto-close sessions, app won't crash immediately, and sessions don't close only on restart.
  3. Final Answer:

    Database connections may remain open, causing resource leaks -> Option B
  4. Quick Check:

    Forgetting close = resource leaks [OK]
Quick Trick: Always close sessions to avoid leaks [OK]
Common Mistakes:
MISTAKES
  • Assuming FastAPI auto-closes sessions
  • Expecting immediate crash on leak
  • Thinking sessions close only on restart

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes