Problem
I just migrated from Sequelize and was quite a fan of Prisma for being type safe. Would Prisma be able to support SELECT FOR UPDATE row locking like Sequelize supports? Understand that Optimistic locking is preferred by Prisma but it would be great if we have the row locking mechanism too.
My problem: When multiple requests call this function concurrently, while first transaction is still processing, the subsequent transaction will see that the user is still active and do the processing again. If we could avoid it by locking the row, that would be great.
function deactivate() {
const user = await prisma.$transaction(async tx => {
const user = await tx.user.findUnique({ where: { id: user.id } });
if (user.status !== 'ACTIVE') {
throw new Error('User not active ...');
}
// some business processing (not long transaction)
return tx.user.update({ where: { id: user.id } }, data: { status: 'DELETED' });
});
await doAfterCommitProcessing(user);
}
Suggested solution
const user = await tx.user.findUnique({ where: { id: user.id }, lock: FOR_UPDATE });
So, the user record with specific id is locked till the transaction is completed.
Alternatives
Additional context
Problem
I just migrated from Sequelize and was quite a fan of Prisma for being type safe. Would Prisma be able to support
SELECT FOR UPDATErow locking like Sequelize supports? Understand that Optimistic locking is preferred by Prisma but it would be great if we have the row locking mechanism too.My problem: When multiple requests call this function concurrently, while first transaction is still processing, the subsequent transaction will see that the user is still active and do the processing again. If we could avoid it by locking the row, that would be great.
Suggested solution
const user = await tx.user.findUnique({ where: { id: user.id }, lock: FOR_UPDATE });So, the user record with specific id is locked till the transaction is completed.
Alternatives
Additional context