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:
@@ -108,7 +108,7 @@ class CustomerService:
|
||||
|
||||
try:
|
||||
db.add(customer)
|
||||
db.commit()
|
||||
db.flush()
|
||||
db.refresh(customer)
|
||||
|
||||
logger.info(
|
||||
@@ -120,7 +120,6 @@ class CustomerService:
|
||||
return customer
|
||||
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
logger.error(f"Error registering customer: {str(e)}")
|
||||
raise CustomerValidationException(
|
||||
message="Failed to register customer", details={"error": str(e)}
|
||||
@@ -310,7 +309,7 @@ class CustomerService:
|
||||
setattr(customer, field, value)
|
||||
|
||||
try:
|
||||
db.commit()
|
||||
db.flush()
|
||||
db.refresh(customer)
|
||||
|
||||
logger.info(f"Customer updated: {customer.email} (ID: {customer.id})")
|
||||
@@ -318,7 +317,6 @@ class CustomerService:
|
||||
return customer
|
||||
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
logger.error(f"Error updating customer: {str(e)}")
|
||||
raise CustomerValidationException(
|
||||
message="Failed to update customer", details={"error": str(e)}
|
||||
@@ -344,7 +342,7 @@ class CustomerService:
|
||||
customer = self.get_customer(db, vendor_id, customer_id)
|
||||
customer.is_active = False
|
||||
|
||||
db.commit()
|
||||
db.flush()
|
||||
db.refresh(customer)
|
||||
|
||||
logger.info(f"Customer deactivated: {customer.email} (ID: {customer.id})")
|
||||
@@ -369,8 +367,6 @@ class CustomerService:
|
||||
customer.total_spent += order_total
|
||||
customer.last_order_date = datetime.utcnow()
|
||||
|
||||
db.commit()
|
||||
|
||||
logger.debug(f"Updated stats for customer {customer.email}")
|
||||
|
||||
def _generate_customer_number(
|
||||
|
||||
Reference in New Issue
Block a user