File size: 743 Bytes
2a09328 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | from collections.abc import AsyncIterator
from sqlalchemy.ext.asyncio import (
AsyncSession,
async_sessionmaker,
create_async_engine,
)
from app.core.config import settings
engine = create_async_engine(
settings.sqlalchemy_database_url,
pool_pre_ping=True,
pool_recycle=300,
echo=False,
)
AsyncSessionFactory = async_sessionmaker(
bind=engine,
class_=AsyncSession,
autoflush=False,
expire_on_commit=False,
)
async def get_database_session() -> AsyncIterator[AsyncSession]:
async with AsyncSessionFactory() as session:
try:
yield session
except Exception:
await session.rollback()
raise
finally:
await session.close() |