views:

215

answers:

1

I have read the article Locking and Concurrency in Java Persistence 2.0, and run the sample application. But i still cant realize the difference between PESSIMISTIC_READ and PESSIMISTIC_WRITE. I tried to modify the code, and where the code using PESSIMISTIC_READ and PESSIMISTIC_WRITE will have the same result that the sql will invoked with "for update".

A: 

One is a read lock and the other is a write lock, or during a read or an update, respectively.

FTA:

  • PESSIMISTIC_READ. The entity manager locks the entity as soon as a transaction reads it. The lock is held until the transaction completes. This lock mode is used when you want to query data using repeatable-read semantics. In other words, you want to ensure that the data is not updated between successive reads. This lock mode does not block other transactions from reading the data.

    PESSIMISTIC_WRITE. The entity manager locks the entity as soon as a transaction updates it. This lock mode forces serialization among transactions attempting to update the entity data. This lock mode is often used when there is a high likelihood of update failure among concurrent updating transactions.

Joseph
thanks, i saw the description, but it still confuse me. When i using PESSIMISTIC_READ and PESSIMISTIC_WRITE, it always get the same result in "show sql" and running result.
paka