Locking for JPA entities without @Version -


env:

java ee 7 jpa 2.1 ejb 3.1 hibernate 4 

recently experiencing data problems in 1 of table. couple of points

  1. the table mapped jpa entity
  2. table entity not have "version" column/attribute.

in other words, there no optimistic locking available table. on doing rca, turned out concurrent data modification issues.

questions :

  1. in such cases @version not available/used (in other words optimistic locking), using singleton repository class option make sure data consistency maintained ?

  2. what pessimistic locking in such cases ?

  3. i believe general use case application (especially legacy) can have tables version column , dont. there known patterns handling tables/entities without version column ?

thanks in advance, rakesh

jpa supports pessimistic locking , free use in case cannot or not want use optimistic locking.

in short, entitymanager provides lock methods lock retrieved entity, , overloaded em.find , em.merge, query.setlockmode provide means supply lock options apply locks atomically @ time when data retrieved db.

however, pessimistic locking, should aware should prevent deadlocks. best way tackle locking @ 1 entity per transaction.

you might consider setting timeout attempt lock entity, transaction not wait long time if entity locked.

in more detail, intelligible explanation of optimistic , pessimistic locking jpa provided here, including differences between read , write lock modes , setting lock timeout.


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -