hibernate - Locking entities or DB on JPA -


does jpa method entitymanager#lock take immediate effect on in-memory managed entity instances or route semantics on-demand db?

locking using entitymanager#lock immediate if use 1 of pessimistic lock types.

using pessimistic lock type routes lock instruction db , waits until either lock succeeds or timeout reached. therefore call blocking , if not successful, transaction rolled back.

the immediate effect on in-memory objects indirect, immediate - if lock not obtained, transaction rolled , not possible finish transaction. makes no sense modify entity objects after this, not persisted. in theory, possible modify , work entity object other plain java object, provided catch locktimeoutexception, not recommend this.

if use optimistic locking, nature of it, transaction rolled after collision detected. can happen at moment entity persisted db (typically @ end of transaction, can happen during intermediate flush). point of optimistic locking deffer locking @ later moment when obvious collision had occurred.

very nice documentation jpa locking mechanism can found in locking in jpa page.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -