The order-placement stock check (read available qty, then insert an "out" deduction) had no row lock, so two concurrent orders for the same product could both pass the availability check before either committed its deduction, overselling the item. Locks each ordered product's productlocations row with SELECT ... FOR UPDATE up front, in a fixed (productid, locationid) order across all items so overlapping concurrent orders contend for locks in the same sequence instead of deadlocking.
49 KiB
49 KiB