Add index on (user_id, shown) in notification table to optimize markNotificationsShown queries and prevent deadlocks. Implement transaction handling in markNotificationsShown method for atomic updates.
This commit is contained in:
@@ -4382,11 +4382,26 @@ class FalukantService extends BaseService {
|
||||
|
||||
async markNotificationsShown(hashedUserId) {
|
||||
const user = await getFalukantUserOrFail(hashedUserId);
|
||||
const [count] = await Notification.update(
|
||||
{ shown: true },
|
||||
{ where: { userId: user.id, shown: false } }
|
||||
);
|
||||
return { updated: count };
|
||||
|
||||
// Use transaction to prevent deadlocks and ensure atomicity
|
||||
const transaction = await sequelize.transaction({
|
||||
isolationLevel: Sequelize.Transaction.ISOLATION_LEVELS.READ_COMMITTED
|
||||
});
|
||||
|
||||
try {
|
||||
const [count] = await Notification.update(
|
||||
{ shown: true },
|
||||
{
|
||||
where: { userId: user.id, shown: false },
|
||||
transaction
|
||||
}
|
||||
);
|
||||
await transaction.commit();
|
||||
return { updated: count };
|
||||
} catch (error) {
|
||||
await transaction.rollback();
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async getPoliticalOfficeHolders(hashedUserId) {
|
||||
|
||||
Reference in New Issue
Block a user