Skip to content

fix(trip-service): resolve race condition in distributed trip assignment lock#21

Merged
Ama2352 merged 1 commit into
mainfrom
feat/redis-lock-optimization
Dec 11, 2025
Merged

fix(trip-service): resolve race condition in distributed trip assignment lock#21
Ama2352 merged 1 commit into
mainfrom
feat/redis-lock-optimization

Conversation

@Ama2352

@Ama2352 Ama2352 commented Dec 11, 2025

Copy link
Copy Markdown
Owner

Fixed critical race condition causing HTTP 500 errors under high concurrency (200 concurrent drivers). The issue occurred when JPA's optimistic locking exception was thrown at transaction commit time, outside the try-catch block.

Root Cause:

  • tripRepository.save() defers DB write until transaction commit
  • Transaction commits AFTER method returns (outside try-catch)
  • StaleObjectStateException thrown at commit time escapes exception handler
  • Results in HTTP 500 instead of graceful 409 Conflict

Solution:

  • Changed save() to saveAndFlush() to force immediate DB write
  • Exception now caught within try-catch block
  • Returns AcceptResult.ALREADY_ASSIGNED (409 Conflict) gracefully
  • Added @slf4j and warning log for optimistic lock conflicts
  • Simplified DriverController to forward responses without redundant error handling

…ent lock

Fixed critical race condition causing HTTP 500 errors under high concurrency
(200 concurrent drivers). The issue occurred when JPA's optimistic locking
exception was thrown at transaction commit time, outside the try-catch block.

Root Cause:
- tripRepository.save() defers DB write until transaction commit
- Transaction commits AFTER method returns (outside try-catch)
- StaleObjectStateException thrown at commit time escapes exception handler
- Results in HTTP 500 instead of graceful 409 Conflict

Solution:
- Changed save() to saveAndFlush() to force immediate DB write
- Exception now caught within try-catch block
- Returns AcceptResult.ALREADY_ASSIGNED (409 Conflict) gracefully
- Added @slf4j and warning log for optimistic lock conflicts
- Simplified DriverController to forward responses without redundant error handling
@Ama2352 Ama2352 merged commit 802474f into main Dec 11, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant