When we use Pessimistic Locking?
While Doing this operation it might be this salary component may be get updated by different user then we might get wrong report
Because there is time leg between 2 queries so might be it get updated by different user.
Because line no 20 gets executed and data get loaded into memory and mean time another user can update this data and that data will not reflect in the loaded data so our report might not be correct.
But this 25 line get executed then it might get different result because while executing this record data get updated
So here we want both the queries get executed in the same transaction and you don't want any of data read in the first query in line 20 modified by any user in the duration of this transaction So we can lock the transaction as follow
The user can read the data but he can't modify the data because it read lock it will not modify the data until this transaction is executed and this lock gets a release and this lock will be released once this method execution get completed
while reading a another use is trying to update the data in line number 30 so hibernate will check if any lock on this data by another use then hibernate will wait until this lock get realised once the lock get realised it will perform the right operation if suppose in case lock is not released until time out then hibernate will through an exception like PessimisticLockingFailureException
Comments
Post a Comment