"""SQLAlchemy implementation of Transaction Manager. This module provides the concrete implementation of TransactionManager using SQLAlchemy async sessions for transaction control. """ from sqlalchemy.ext.asyncio import AsyncSession from app.application.interfaces import TransactionManager class SessionTransactionManager(TransactionManager): """SQLAlchemy Session-based Transaction Manager. Implements transaction control using SQLAlchemy async session. Tracks commit state to prevent duplicate commits. Attributes: _session: SQLAlchemy async session for transactions. _committed: Flag indicating if transaction was committed. Example: >>> tx_manager = SessionTransactionManager(session) >>> await tx_manager.commit() """ def __init__(self, session: AsyncSession) -> None: """Initialize transaction manager. Args: session: SQLAlchemy async session instance. """ self._session = session self._committed: bool = False async def commit(self) -> None: """Commit the current transaction. Persists all pending changes to the database. Only commits once - subsequent calls are no-ops. """ if not self._committed: await self._session.commit() self._committed = True async def rollback(self) -> None: """Rollback the current transaction. Discards all pending changes. Only rolls back if not already committed. """ if not self._committed: await self._session.rollback()