Reputation: 2585
According to the JPA 2.1 specification...
The lock modes
PESSIMISTIC_READ
,PESSIMISTIC_WRITE
, andPESSIMISTIC_FORCE_INCREMENT
are used to immediately obtain long-term database locks.
I assume a pessimistic lock will always trigger a SELECT ... FOR UPDATE
SQL on the database, no matter what lock-mode is used. Now three questions on that:
SELECT ... FOR UPDATE
locked the rows. Locked rows cannot be updated by any other transaction except the transaction which locked it?Upvotes: 1
Views: 2199
Reputation: 7710
For the question 1 and 2, your assumptions are correct:
Yes - pessimistic lock generally uses SELECT ... FOR UPDATE
, as most databases and JPA implementations only support this type of lock. In this case there is no difference between READ and WRITE block, and JPA specification allows it as long as both behave as WRITE locks.
Yes - locked rows cannot be modified by any other transaction. In case of WRITE lock (and most time also for READ lock - se answer for 1), locked rows cannot be read also until the lock is released. Note that other unlocked rows in the same table are free to be read and modified.
To answer also question 3:
Upvotes: 6