Check for proper database session lifecycle management
Scans codebase for database session lifecycle violations across services, repositories, and routers.
/plugin marketplace add adelabdelgawad/full-stack/plugin install session-management@full-stackIMPORTANT: When this command is invoked, you MUST actually SCAN the codebase and REPORT findings. Do NOT just describe what to check.
Use Glob to find relevant files:
Routers: {backend}/api/routers/*.py
Services: {backend}/api/services/*_service.py
Repositories: {backend}/api/repositories/*_repository.py
Check services for storing sessions as instance variables:
❌ VIOLATION:
class UserService:
def __init__(self, session: AsyncSession):
self._session = session # WRONG! Don't store sessions
✅ CORRECT:
class UserService:
def __init__(self):
self._repo = UserRepository()
# NO session stored
Check that sessions are passed as method parameters:
❌ VIOLATION:
async def create_user(self, data):
# Uses stored self._session
return await self._repo.create(self._session, data)
✅ CORRECT:
async def create_user(self, session: AsyncSession, data):
# Session passed as parameter
return await self._repo.create(session, data)
Check repositories use flush() not commit():
❌ VIOLATION:
await session.commit() # Repositories should NOT commit
✅ CORRECT:
await session.flush() # Let router handle commit
await session.refresh(entity)
Check routers have proper try/except with commit/rollback:
❌ VIOLATION:
@router.post("/users")
async def create_user(data, session = Depends(get_session)):
return await UserService().create_user(session, data) # No transaction handling!
✅ CORRECT:
@router.post("/users")
async def create_user(data, session = Depends(get_session)):
try:
result = await UserService().create_user(session, data)
await session.commit()
return result
except Exception:
await session.rollback()
raise
FORMAT YOUR OUTPUT EXACTLY LIKE THIS:
╔══════════════════════════════════════════════════════════════╗
║ SESSION MANAGEMENT COMPLIANCE REPORT ║
╚══════════════════════════════════════════════════════════════╝
📋 SERVICES CHECKED
────────────────────────
✅ user_service.py - Clean (no session storage)
✅ role_service.py - Clean
❌ order_service.py - VIOLATION: Stores session in __init__
📋 REPOSITORIES CHECKED
────────────────────────
✅ user_repository.py - Clean (uses flush)
❌ order_repository.py - VIOLATION: Uses commit()
📋 ROUTERS CHECKED
────────────────────────
✅ user_router.py - Has transaction handling
❌ order_router.py - VIOLATION: No transaction handling
📊 VIOLATIONS DETAIL
────────────────────────
order_service.py:
Line 12: def __init__(self, session: AsyncSession):
Line 13: self._session = session ❌ Session stored as instance variable
Line 25: async def create(self, data): ❌ Missing session parameter
order_repository.py:
Line 34: await session.commit() ❌ Should use flush()
order_router.py:
Line 45: return await OrderService().create(session, data)
❌ Missing try/except with commit/rollback
📊 SUMMARY
────────────────────────
Services clean: 4/5
Repositories clean: 5/6
Routers clean: 4/6
Total violations: 5
🔧 REQUIRED FIXES
────────────────────────
1. order_service.py:
- Remove session from __init__
- Add session parameter to all methods
2. order_repository.py:
- Replace commit() with flush()
3. order_router.py:
- Add try/except with commit/rollback
If violations are found, ask: "Would you like me to fix the session management violations?"
If user agrees:
# Before
class OrderService:
def __init__(self, session: AsyncSession):
self._session = session
self._repo = OrderRepository()
async def create(self, data):
return await self._repo.create(self._session, data)
# After
class OrderService:
def __init__(self):
self._repo = OrderRepository()
async def create(self, session: AsyncSession, data):
return await self._repo.create(session, data)
# Before
await session.commit()
# After
await session.flush()
await session.refresh(entity)
# Before
@router.post("/orders")
async def create_order(data, session = Depends(get_session)):
return await OrderService().create(session, data)
# After
@router.post("/orders")
async def create_order(data, session = Depends(get_session)):
try:
result = await OrderService().create(session, data)
await session.commit()
return result
except Exception:
await session.rollback()
raise
Storing sessions as instance variables causes:
This command MUST:
DO NOT just tell the user to run a script. PERFORM THE CHECK.