Skip to main content
Glama
session.py1.22 kB
"""Database session management. This module provides async session factories and dependency injection functions for database access in the application. """ from collections.abc import AsyncGenerator from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker from src.database.base import get_engine def get_session_factory() -> async_sessionmaker[AsyncSession]: """Create an async session factory. Returns: Async session factory configured with the database engine. """ return async_sessionmaker( bind=get_engine(), class_=AsyncSession, expire_on_commit=False, autoflush=False, ) AsyncSessionLocal = get_session_factory() async def get_db() -> AsyncGenerator[AsyncSession, None]: """Get a database session for dependency injection. Yields: AsyncSession instance for database operations. Note: The session is automatically closed when the context exits. """ async with AsyncSessionLocal() as session: try: yield session await session.commit() except Exception: await session.rollback() raise finally: await session.close()

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/Fu-Jie/MCP-OPENAPI-DOCX'

If you have feedback or need assistance with the MCP directory API, please join our Discord server