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:
2025-12-06 18:34:41 +01:00
parent 5d40551d98
commit 3520bcb069
33 changed files with 118 additions and 119 deletions

View File

@@ -113,7 +113,7 @@ class VendorDomainService:
)
db.add(new_domain)
db.commit()
db.flush()
db.refresh(new_domain)
logger.info(f"Domain {normalized_domain} added to vendor {vendor_id}")
@@ -126,10 +126,8 @@ class VendorDomainService:
InvalidDomainFormatException,
ReservedDomainException,
):
db.rollback()
raise
except Exception as e:
db.rollback()
logger.error(f"Error adding domain: {str(e)}")
raise ValidationException("Failed to add domain")
@@ -223,17 +221,15 @@ class VendorDomainService:
if domain_update.is_active is not None:
domain.is_active = domain_update.is_active
db.commit()
db.flush()
db.refresh(domain)
logger.info(f"Domain {domain.domain} updated")
return domain
except (VendorDomainNotFoundException, DomainNotVerifiedException):
db.rollback()
raise
except Exception as e:
db.rollback()
logger.error(f"Error updating domain: {str(e)}")
raise ValidationException("Failed to update domain")
@@ -257,16 +253,13 @@ class VendorDomainService:
vendor_id = domain.vendor_id
db.delete(domain)
db.commit()
logger.info(f"Domain {domain_name} deleted from vendor {vendor_id}")
return f"Domain {domain_name} deleted successfully"
except VendorDomainNotFoundException:
db.rollback()
raise
except Exception as e:
db.rollback()
logger.error(f"Error deleting domain: {str(e)}")
raise ValidationException("Failed to delete domain")
@@ -312,7 +305,7 @@ class VendorDomainService:
# Verification successful
domain.is_verified = True
domain.verified_at = datetime.now(UTC)
db.commit()
db.flush()
db.refresh(domain)
logger.info(f"Domain {domain.domain} verified successfully")