Async context manager to yield a database session. This context manager ensures that any changes made during the session are rolled back in case of an exception, and the session is properly closed to prevent memory leaks. Yields: AsyncSession: S
(self)
| 65 | |
| 66 | @asynccontextmanager |
| 67 | async def get_core_session(self) -> AsyncGenerator[AsyncSession, None]: |
| 68 | """ |
| 69 | Async context manager to yield a database session. |
| 70 | |
| 71 | This context manager ensures that any changes made during the session are |
| 72 | rolled back in case of an exception, and the session is properly closed to |
| 73 | prevent memory leaks. |
| 74 | |
| 75 | Yields: |
| 76 | AsyncSession: SQLAlchemy async session for database operations. |
| 77 | |
| 78 | Raises: |
| 79 | Exception: Any exception that occurs within the context will be |
| 80 | re-raised after the session rollback. |
| 81 | """ |
| 82 | |
| 83 | session = self.async_core_session() |
| 84 | try: |
| 85 | yield session |
| 86 | except Exception as e: |
| 87 | await session.rollback() |
| 88 | raise e |
| 89 | finally: |
| 90 | await session.close() |
| 91 | |
| 92 | @asynccontextmanager |
| 93 | async def get_tracing_session(self) -> AsyncGenerator[AsyncSession, None]: |