Concurrency & Locking Controls Skill
Purpose
Protect financial domain state transitions against concurrent race conditions, lost updates, and double-spends.
1. Rules of Engagement
CONDITIONAL
- Optimistic Locking (
@Version): Apply JPA@Versionoptimistic locking fields on write-mutable domain entities subject to concurrent state updates (Transaction,AccountBalance,DisputeRecord). Non-mutated or append-only entities do not require@Version. - Pessimistic Locking (
SELECT FOR UPDATE): UsePESSIMISTIC_WRITEfor high-contention financial balance updates where optimistic lock retries cause high abort rates.
DOMAIN SEMANTIC LOCKING RULE
- When
OptimisticLockExceptionor state conflict occurs:- Do NOT blindly retry mutation in a loop.
- If the entity state was already transitioned to the desired final state by a concurrent worker, return the current state or 409 Conflict based on business rules.