23 lines
749 B
Python
23 lines
749 B
Python
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
|
|
from config import DATABASE_URL
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
engine = create_async_engine(DATABASE_URL, echo=False, pool_pre_ping=True)
|
|
async_session_maker = async_sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
|
|
|
|
|
|
async def get_session() -> AsyncSession:
|
|
"""Получить асинхронную сессию БД"""
|
|
async with async_session_maker() as session:
|
|
try:
|
|
yield session
|
|
except Exception as e:
|
|
logger.error(f"Ошибка в сессии БД: {e}")
|
|
await session.rollback()
|
|
raise
|
|
finally:
|
|
await session.close()
|
|
|