The application
Hi,
We have an application (J2EE/Hibernate/JPA) with several users making actions on a common entity.
To simplify, let's say this application is like Google docs : A shared document on which many users can update at the same time.
We have chosen optmistic lock to handle concurrency :
- Sentences are separate entities
- There is a low chance that several users will update the same sentence at the same time
- In that case, it is ok for one of the user to receive a message "sorry another user tried to edit the same sentence"
Background processes
So far, so good.
But now, we have added background processes (quite fast ones) to this application. They regularly make changes (let say it replace any occurence of a word by another).
It is ok for those job to fail. Their task is not urgent, and they can try the same task the next time (10 seconds after).
The problem with the optimistic lock in that situation is that now a single user cannot perform anymore long actions on te whole document.
Indeed, if a user changes the font of the whole document (on all sentences), and this action takes a while (> 10 sec), then in the meantime the background process will change some words, and the longer action (= the user action : change font) will fail on concurrent access.
This is not acceptable to show this mesage to the user : "Your action failed because some technical process was running at same time".
And since the background process could retry later, we would rather have it fail.
The question
How do we setup pessimistic lock for some actions / actors , within a optimistic lock approach ?
Potential solutions
In order to keep the optimistic approch for the users, we have thought of the following solution :
- Create a "user action" flag, that would be set during any action of any user
- A job will not start until this flag is "off"
- At the end of any running processes, check if the flag is on : If so, cancel this action / rollback.
This way, the processes will only be able to run at idle time, while the user is not doing anything.
Help
Is this a good approach ? We could not find any article / discussion about good practices for hybrid types of locks.