refactor: move transaction management from services to API endpoints
- Services now use db.flush() instead of db.commit() for database operations - API endpoints handle transaction commit after service calls - Remove db.rollback() from services (let exception handlers manage this) - Ensures consistent transaction boundaries at API layer This pattern gives API endpoints full control over when to commit, allowing for better error handling and potential multi-operation transactions. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -163,7 +163,7 @@ class AdminSettingsService:
|
||||
)
|
||||
|
||||
db.add(setting)
|
||||
db.commit()
|
||||
db.flush()
|
||||
db.refresh(setting)
|
||||
|
||||
logger.info(f"Setting '{setting.key}' created by admin {admin_user_id}")
|
||||
@@ -171,10 +171,8 @@ class AdminSettingsService:
|
||||
return AdminSettingResponse.model_validate(setting)
|
||||
|
||||
except ValidationException:
|
||||
db.rollback()
|
||||
raise
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
logger.error(f"Failed to create setting: {str(e)}")
|
||||
raise AdminOperationException(
|
||||
operation="create_setting", reason="Database operation failed"
|
||||
@@ -205,7 +203,7 @@ class AdminSettingsService:
|
||||
setting.last_modified_by_user_id = admin_user_id
|
||||
setting.updated_at = datetime.now(UTC)
|
||||
|
||||
db.commit()
|
||||
db.flush()
|
||||
db.refresh(setting)
|
||||
|
||||
logger.info(f"Setting '{setting.key}' updated by admin {admin_user_id}")
|
||||
@@ -213,10 +211,8 @@ class AdminSettingsService:
|
||||
return AdminSettingResponse.model_validate(setting)
|
||||
|
||||
except ValidationException:
|
||||
db.rollback()
|
||||
raise
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
logger.error(f"Failed to update setting {key}: {str(e)}")
|
||||
raise AdminOperationException(
|
||||
operation="update_setting", reason="Database operation failed"
|
||||
@@ -244,14 +240,12 @@ class AdminSettingsService:
|
||||
|
||||
try:
|
||||
db.delete(setting)
|
||||
db.commit()
|
||||
|
||||
logger.warning(f"Setting '{key}' deleted by admin {admin_user_id}")
|
||||
|
||||
return f"Setting '{key}' successfully deleted"
|
||||
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
logger.error(f"Failed to delete setting {key}: {str(e)}")
|
||||
raise AdminOperationException(
|
||||
operation="delete_setting", reason="Database operation failed"
|
||||
|
||||
Reference in New Issue
Block a user