Sunday, 2 May 2021

multiple requests to node express mysql synchronization issue

Using expressJS I have an endpoint which is connected to a mysql DB where I am making a query and getting a set of data and assigning the first returned result to a user by updating a field to the user id.

The concept is that single row can only be assigned to a single user until the user has processed the row by hitting another endpoint.

Here is the query which gets the latest not processed rows:

const notProcessed = await knex('rows')
                .select('*'')
                .whereRaw(`status='Not-Processed' and (assignedTo=0 or assignedTo=${user.id})`)
                .orderByRaw('createdAt asc')
                .first();

and after I am updating the row to the user by doing:

await knex('rows').update({ assignedTo: user.id }).where({ id: notProcessed.id })

When multiple requests are sent to the endpoint sometimes I have the same row assigned to the more than 1 person.

Can't seem to find the reason or a solution for this. can anyone explain why this is happening and what is the best approach to solve this?



from multiple requests to node express mysql synchronization issue

No comments:

Post a Comment