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?

    • 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.

    • 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?

    • 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?

    • 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

This site uses Akismet to reduce spam. Learn how your comment data is processed.