Files
rentman-clone/backend/app/db/session.py
T

32 lines
865 B
Python
Raw Normal View History

"""Async SQLAlchemy engine and session factory."""
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
from sqlalchemy.pool import NullPool
from app.core.config import settings
engine = create_async_engine(
settings.DATABASE_URL,
echo=settings.APP_DEBUG,
poolclass=NullPool, # SQLite doesn't support connection pooling across threads
)
AsyncSessionFactory = async_sessionmaker(
engine,
class_=AsyncSession,
expire_on_commit=False,
)
async def get_async_session() -> AsyncSession:
"""Dependency that provides an async database session."""
async with AsyncSessionFactory() as session:
try:
yield session
await session.commit()
except Exception:
await session.rollback()
raise
finally:
await session.close()