This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes what are the semantics of PESSISMISTIC_READ AND PESSISTIMIC_WRIE Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "what are the semantics of PESSISMISTIC_READ AND PESSISTIMIC_WRIE" Watch "what are the semantics of PESSISMISTIC_READ AND PESSISTIMIC_WRIE" New topic
Author

what are the semantics of PESSISMISTIC_READ AND PESSISTIMIC_WRIE

Elhanan Maayan
Ranch Hand

Joined: May 04, 2009
Posts: 111
from the java doc it says "A lock with LockModeType.PESSIMISTIC_WRITE can be obtained on an entity instance to force serialization among transactions attempting to update the entity data. " WTH does that MEAN? among who's transactions? does it refer to an object moving between session when one transaction is closed and then other is opened? another source refers to it as a lock when the object is updated, meaning what? when i'm using a setter? persist ? flush? is PESSIMISTIC_WRITE a more relaxed version between the classic pessimistic read and the optimistic lock?

from what i understand PESSIMISTIC_READ is the "classic" pessimistic lock where the record is locked using a for update statement, but, both use for update, so i'm assuming you can't really use both locks with different users, so what's left is the combination PESSIMISTIC_WRITE, and a regular optimistic locking, and what are the implications of using it over READ version?
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
See, http://en.wikibooks.org/wiki/Java_Persistence/Locking#JPA_2.0_Locking

According to the Spec PESSISMISTIC_READ only blocks PESSISTIMIC_WRITE locks, not other PESSISMISTIC_READ locks.

In practice the difference between PESSISMISTIC_READ AND PESSISTIMIC_WRITE depends on the database and the JPA provider. I believe that most just use FOR UPDATE for both options and they are treated as the same (so PESSISMISTIC_READ will block other PESSISMISTIC_READ locks).


TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
Guy Mograbi
Greenhorn

Joined: Jan 10, 2011
Posts: 4

In computer science, serializing means turn data into a stream.

However, in English it can also be used to state "one after the other".
You don't want multiple writes to happen at the same time, but you do want them to happen one after the other ==> hence you want to serialize them.

However, that still doesn't answer some questions, their phrasing is not ideal.
To keep things simple, I understand it works as follows:

You can have many readers at the same time.
When there are readers, you can't have writers.
Once there are no more readers, a write can get the lock.
Only one writer can have the lock at any given moment.

This answers all scenarios. Let me know if there's something else I can help you with.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: what are the semantics of PESSISMISTIC_READ AND PESSISTIMIC_WRIE
 
Similar Threads
Question to Kyle Brown
Spring and Transactions
Select FOR UPDATE native query VS. entityManager.lock()?
JPA - Optimistic Lock and JTA Transaction
Pessimistic Locking