Am trying to implement Optimistic lock in our Applications and gave @Version JPA

Am trying to implement Optimistic lock in our Applications and gave @Version JPA Anotations to the version_id integer field in a CDT. CDT has many parent to child relationship. Value of the version_id is not incremented even after writing the particular transaction to DB2. Could you please let us know whether we need to define the column in DB2 as Auto Increment to make it work or it should work even without that. Thanks in advance.

OriginalPostID-176763

OriginalPostID-176763

  Discussion posts and replies are publicly visible

Parents
  • - Make sure your @Version field in the XSD is marked as "NOT NULL".

    - Also keep in mind that the version will be updated only if the value to write is different from the originally retrieved value. Since that's the purpose of the @Version annotation.

    The @Version annotation is used for optimistic locking to detect and prevent collisions through a version-check upon commit; if the version number retrieved is different from the current one in the database the update fails with an OptimisticLockException, and you cannot write the values back because you now have dirty data; old data which has been already overridden by somebody else.
Reply
  • - Make sure your @Version field in the XSD is marked as "NOT NULL".

    - Also keep in mind that the version will be updated only if the value to write is different from the originally retrieved value. Since that's the purpose of the @Version annotation.

    The @Version annotation is used for optimistic locking to detect and prevent collisions through a version-check upon commit; if the version number retrieved is different from the current one in the database the update fails with an OptimisticLockException, and you cannot write the values back because you now have dirty data; old data which has been already overridden by somebody else.
Children
No Data