From dbb3cf622f476d4734318b5a4981d356941e4bd4 Mon Sep 17 00:00:00 2001 From: EduBrain Dev Date: Tue, 14 Apr 2026 21:04:00 +0800 Subject: [PATCH] fix issue --- app/database.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/database.py b/app/database.py index d9d3cd3..ea46a04 100644 --- a/app/database.py +++ b/app/database.py @@ -50,9 +50,15 @@ async_session_factory = async_sessionmaker( async def get_db() -> AsyncGenerator[AsyncSession, None]: """ FastAPI 依赖项:获取异步数据库会话。 + 请求正常结束后自动 commit,异常时自动 rollback。 """ async with async_session_factory() as session: - yield session + try: + yield session + await session.commit() + except Exception: + await session.rollback() + raise async def close_db() -> None: