|
1234567891011121314151617 |
- from typing import AsyncGenerator
- from sqlalchemy.exc import SQLAlchemyError
- from sqlalchemy.ext.asyncio import AsyncSession
-
- from app.db.base import AsyncSessionLocal
-
-
- async def get_db_session() -> AsyncGenerator[AsyncSession, None]:
- async with AsyncSessionLocal() as session:
- try:
- yield session
- await session.commit()
- except SQLAlchemyError as e:
- await session.rollback()
- raise
- finally:
- await session.close()
|