修复:避免首事件间隙锁死锁

This commit is contained in:
焦龙言
2026-07-26 14:24:55 +08:00
parent 6c2a5fa344
commit 317a0fbf68
4 changed files with 54 additions and 14 deletions
@@ -93,16 +93,21 @@ class InventoryDao:
event: BizPalletEvent,
) -> BizPalletEvent:
"""在调用方持有库存行锁时,为托盘追加下一序号事件。"""
sequence_result = await db.execute(
select(BizPalletEvent.event_seq)
.where(BizPalletEvent.pallet_id == inventory.pallet_id)
.order_by(BizPalletEvent.event_seq.desc())
.limit(1)
.with_for_update()
)
last_sequence = sequence_result.scalar_one_or_none()
last_sequence = 0
if inventory.last_event_id is not None:
sequence_result = await db.execute(
select(BizPalletEvent.event_seq)
.where(
BizPalletEvent.event_id == inventory.last_event_id,
BizPalletEvent.pallet_id == inventory.pallet_id,
)
.with_for_update()
)
last_sequence = sequence_result.scalar_one_or_none()
if last_sequence is None:
raise RuntimeError('托盘库存事件指针不一致')
event.pallet_id = inventory.pallet_id
event.event_seq = (last_sequence or 0) + 1
event.event_seq = last_sequence + 1
db.add(event)
await db.flush()
inventory.last_event_id = event.event_id