Bird
0
0

Find the bug in this FastAPI connection pooling setup: ```python engine = create_engine("postgresql://user:pass@localhost/db", pool_size=10, max_overflow=5) SessionLocal = sessionmaker(bind=engine) @app.get("/data") async def get_data(): db = SessionLocal() result = db.execute("SELECT * FROM data_table") return result.fetchall() ```

medium📝 Debug Q7 of 15
FastAPI - Database Integration
Find the bug in this FastAPI connection pooling setup: ```python engine = create_engine("postgresql://user:pass@localhost/db", pool_size=10, max_overflow=5) SessionLocal = sessionmaker(bind=engine) @app.get("/data") async def get_data(): db = SessionLocal() result = db.execute("SELECT * FROM data_table") return result.fetchall() ```
ASession is not closed after use, risking connection leaks
Bmax_overflow cannot be greater than pool_size
CUsing async def with synchronous DB calls causes errors
Dcreate_engine parameters are invalid for PostgreSQL
Step-by-Step Solution
Solution:
  1. Step 1: Check session lifecycle management

    Session is created but never closed or released back to pool.
  2. Step 2: Understand consequences

    Not closing sessions causes connection leaks and pool exhaustion over time.
  3. Final Answer:

    Session is not closed after use, risking connection leaks -> Option A
  4. Quick Check:

    Always close sessions to avoid leaks [OK]
Quick Trick: Close sessions after queries to prevent leaks [OK]
Common Mistakes:
MISTAKES
  • Assuming max_overflow limit is pool_size
  • Ignoring need to close sessions
  • Confusing async with sync DB calls

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes