Fix database session context manager errors

- Add explicit commit/rollback in session dependencies
- Prevents RuntimeError: generator didn't stop after athrow()
- Ensures proper transaction cleanup on exceptions
This commit is contained in:
2026-01-23 18:35:15 +01:00
parent e09bb0451c
commit 2b904fd01e

View File

@@ -130,14 +130,15 @@ async def get_database_session() -> AsyncGenerator:
detail="Database functionality not installed"
)
try:
async with get_db_session() as session:
async with get_db_session() as session:
try:
yield session
except RuntimeError as e:
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail=f"Database not available: {str(e)}"
)
# Auto-commit on successful completion
await session.commit()
except Exception:
# Auto-rollback on error
await session.rollback()
raise
async def get_optional_database_session() -> AsyncGenerator:
@@ -169,12 +170,16 @@ async def get_optional_database_session() -> AsyncGenerator:
yield None
return
try:
async with get_db_session() as session:
# Use get_db_session context manager properly
async with get_db_session() as session:
try:
yield session
except (ImportError, RuntimeError):
# Database became unavailable - this shouldn't happen but handle it
yield None
# Auto-commit on successful completion
await session.commit()
except Exception:
# Auto-rollback on error
await session.rollback()
raise
def get_current_user(