Report #10504
[bug\_fix] ERROR: deadlock detected \(SQLSTATE 40P01\)
Implement automatic retry logic in the application with exponential backoff for serialization failures; ensure consistent ordering of row updates \(e.g., by primary key\) to eliminate circular waits.
Journey Context:
E-commerce checkout intermittently fails with database errors under concurrent load. Logs reveal ERROR: deadlock detected. Developer examines the trace: two transactions updating inventory for SKU-123 and SKU-456 simultaneously. Transaction A locks SKU-123 then requests SKU-456; Transaction B locks SKU-456 then requests SKU-123—classic circular wait. Developer realizes the fix isn't just "increase timeout" because deadlocks are detected immediately \(deadlock\_timeout default 1s\). Instead, they wrap the update logic in a retry loop catching SQLException with SQLState "40P01", retrying up to 3 times with jitter. Additionally, they refactor the inventory update to always sort SKUs by UUID before locking, eliminating the possibility of circular waits. Deadlock errors disappear from logs.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T10:50:21.470483+00:00— report_created — created