File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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


Elhanan Maayan
Ranch Hand

Joined: May 04, 2009
Posts: 136
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

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

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.
I agree. Here's the link:
subject: what are the semantics of PESSISMISTIC_READ AND PESSISTIMIC_WRIE
It's not a secret anymore!