Difference between LockModeType Jpa

I am confused about the working of LockModeTypes in JPA:

  1. LockModeType.Optimistic

    • it increments the version while committing.
    • Question here is : If I have version column in my entity and if I don’t specify this lock mode then also it works similarly then what is the use of it?
  2. LockModeType.OPTIMISTIC_FORCE_INCREMENT

    • Here it increments the version column even though the entity is not updated.
    • but what is the use of it if any other process updated the same row before this transaction is committed? this transaction is anyways going to fail. so what is the use of this LockModeType.
  3. LockModeType.PESSIMISTIC_READ

    • This lock mode issues a select for update nowait(if no hint timeout specified)..
    • so basically this means that no other transaction can update this row until this transaction is committed, then its basically a write lock, why its named a Read lock?
  4. LockModeType.PESSIMISTIC_WRITE

    • This lock mode also issues a select for update nowait (if no hint timeout specified).
    • Question here is what is the difference between this lock mode and LockModeType.PESSIMISTIC_READ as I see both fires same queries?
  5. LockModeType.PESSIMISTIC_FORCE_INCREMENT

    • this does select for update nowait (if no hint timeout specified) and also increments the version number.
    • I totally didn’t get the use of it.
    • why a version increment is required if for update no wait is there?


Source: oracle

Leave a Reply