A Database Connection for Every Request

A get_db dependency opens a SQLite connection for each request, hands it to the route with yield, commits when the route succeeds, rolls back when it raises, and always closes it, so plain def routes ask for a connection with Depends instead of opening and closing their own.

By

Updated

Cheat sheet: A Database Connection for Every Request
Category
FastAPI
Level
Journeyman
Tags
Dependency Injection in FastAPI, Dependency with yield, Depends(), fastapi, rest-api, ROLLBACK (sql), Transaction