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:
Torsten Schulz (local)
2025-12-18 15:04:37 +01:00
parent 8e1e0968ae
commit 6a9b2b8d1d
2 changed files with 50 additions and 5 deletions

View File

@@ -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) {