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:
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user