Two requests update the same two rows in a transaction and one fails with a deadlock about once a day:
await db.$transaction(async (tx) => {
await tx.account.update({ where: { id: fromId }, data: { balance: { decrement: amount } } });
await tx.account.update({ where: { id: toId }, data: { balance: { increment: amount } } });
});
Transfer A→B and B→A at the same time deadlock. How do I avoid this?
- Reacted by