In our web application, we have the following snippet of code which is invoked with a button:
if (!isResponsibleSet(request))
{
edit(request, [("responsibleUser"):(user)];
}
request is the object we're editing
user is the user which is about to become responsible for the request
The problem is that sometimes there more that just one user clicks the same button at the same time.
Unfortunately, in the scenario described above the following happens:
- User 1 clicks the button
- User 2 clicks the button
- The if statement evalutes true for User 1
- The if statement evalutes true for User 2
- User 1 edits the request and become the responsible
- User 2 edits the request and become the responsible
The if statement evaluates true for User 2 just because editing takes so long so responsible user isn't set in the database yet
We need to eliminate that possibility, and make just one and only one user responsible for the request, not allowing anybody else edit it (just add one more condition to the if block). Preferably throw an exception that the request is currently under editing already
How to implement such behaviour in the code?