Fix production deadlock
This commit is contained in:
@@ -246,6 +246,33 @@ class DatabaseManager:
|
||||
def __repr__(self) -> str:
|
||||
return f"DatabaseManager(service='{self.service_name}', type='{self._get_database_type()}')"
|
||||
|
||||
async def execute(self, query: str, *args, **kwargs):
|
||||
"""
|
||||
Execute a raw SQL query with proper session management
|
||||
Note: Use this method carefully to avoid transaction conflicts
|
||||
"""
|
||||
from sqlalchemy import text
|
||||
|
||||
# Use a new session context to avoid conflicts with existing sessions
|
||||
async with self.get_session() as session:
|
||||
try:
|
||||
# Convert query to SQLAlchemy text object if it's a string
|
||||
if isinstance(query, str):
|
||||
query = text(query)
|
||||
|
||||
result = await session.execute(query, *args, **kwargs)
|
||||
# For UPDATE/DELETE operations that need to be committed
|
||||
if query.text.strip().upper().startswith(('UPDATE', 'DELETE', 'INSERT')):
|
||||
await session.commit()
|
||||
|
||||
return result
|
||||
except Exception as e:
|
||||
# Only rollback if it was a modifying operation
|
||||
if isinstance(query, str) and query.strip().upper().startswith(('UPDATE', 'DELETE', 'INSERT')):
|
||||
await session.rollback()
|
||||
logger.error("Database execute failed", error=str(e))
|
||||
raise
|
||||
|
||||
|
||||
# ===== CONVENIENCE FUNCTIONS =====
|
||||
|
||||
|
||||
Reference in New Issue
Block a user