274
request scope
also unit of work, scoped session
One database session for the request. Close it before you return. Do not share it.
What is request scope?
A request scope is the unit of work for one HTTP request or one job: open a session, do the work, commit or roll back, close in a finally. A session is not safe across threads or across requests. A global session races. A session cached on the application object leaks the first request’s transaction into the second.
Why does request scope matter when vibe coding?
The draft creates one session at startup and uses it in every handler. Two requests interleave. Or it opens a session per row. Or a background task uses the request session after the response has closed it.
How do you do request scope?
Open at the boundary, close in finally. One commit. A job gets its own scope. Do not store the session in a global or pass it to fire-and-forget work.
How do you ask a model for request scope?
Open one database session per request for (handler). Commit or roll back once. Close it in a finally. Do not share it across requests or threads. A background job opens its own session.
What goes wrong with request scope?
A middleware that opens the session and a handler that also opens one. You now have two transactions and you commit the wrong one.