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
- the table mapped jpa entity
- 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 :
in such cases @version not available/used (in other words optimistic locking), using singleton repository class option make sure data consistency maintained ?
what pessimistic locking in such cases ?
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
Post a Comment