# 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.

![Cheat sheet: A Database Connection for Every Request](https://fsdtrainingmedia.blob.core.windows.net/public/images/2026/11/fb18524b367f40bf8a6fb6a32b35e969.webp)

- Category: FastAPI
- Level: Journeyman
- Tags: Dependency Injection in FastAPI, Dependency with yield, Depends(), fastapi, rest-api, ROLLBACK (sql), Transaction
- Author: Gregor Dzierzon
- Updated: November 23, 2026

Source: https://www.ask-codey.com/cheat-sheets/fastapi-data-get-db-dependency-cheat-sheet
