mirror of
https://github.com/jhd3197/ServerKit.git
synced 2026-10-30 16:47:01 +09:00
24 lines
682 B
Python
24 lines
682 B
Python
"""Small transaction boundary for top-level service use cases."""
|
|
|
|
from contextlib import contextmanager
|
|
|
|
from app import db
|
|
|
|
|
|
@contextmanager
|
|
def unit_of_work(session=None):
|
|
"""Commit one use case atomically and always roll back a failed attempt.
|
|
|
|
Lower-level helpers called inside this context may flush, but must not
|
|
commit. External side effects belong after the context so a remote call is
|
|
never mistaken for part of the database transaction.
|
|
"""
|
|
|
|
active_session = session if session is not None else db.session
|
|
try:
|
|
yield active_session
|
|
active_session.commit()
|
|
except Exception:
|
|
active_session.rollback()
|
|
raise
|