fix: wrap card creation in transaction to prevent race condition#349
Conversation
| }, | ||
| }, | ||
| }); | ||
| return tx.card.create({ |
There was a problem hiding this comment.
How come multiple return statements inside single function?
|
hey @ShantKhatri Refactored the transaction callback to use a clearer control flow with a single explicit final return statement for better readability and maintainability. Updated in the latest commit. |
Now we have so many unnecessary changes, that's not acceptable. If that repeats continuously, will need to close the PRs. |
f6efa3d to
fec20ff
Compare
fec20ff to
e9e2fce
Compare
|
hey @ShantKhatri Cleaned up the PR and removed unrelated changes. The branch now contains only the minimal transaction fix required for resolving the race condition issue. |
Signed-off-by: dinesh <midoriya54378@gmail.com>
Summary
Fixes a race condition in card creation where concurrent requests could create multiple default cards for the same user.
Changes Made
app.prisma.card.countwith transactionaltx.card.countapp.prisma.card.createwith transactionaltx.card.createWhy This Fix Is Needed
Previously, the count check and card creation were executed as separate database operations. Concurrent requests could both read
cardCount === 0and create multiple cards marked asisDefault: true.Using a Prisma transaction removes the race window and preserves data integrity.
Result
Closes #333