java - How to get a PessimisticLockException with JPA -
i trying jpa's support of database locking in order avoid concurrent requests in record. in scenario need use pesssimistic lock. got using following approach:
entitymanager em; ... map<string,object> props = new hashmap<string,object>(); props.put("javax.persistence.lock.timeout", 0); em.find(myentity.class, id, lockmodetype.pessimistic_read, props) but approach handle locktimeoutexception instead of pessimisticlockexception. how handle pessimisticlockexception specifically?
according spec, pessimisticlockexception occurs on transaction-level rollback:
when lock cannot obtained, , database locking failure results in transaction-level rollback, provider must throw pessimisticlockexception , ensure jta transaction or entitytransaction has been marked rollback.
your transaction seems consists of single data retrieval query , persistence provider considers if lock error occurs on query, rollback considered statement-level rollback. according spec result in locktimeoutexception:
when lock cannot obtained, , database locking failure results in statement-level rollback, provider must throw locktimeoutexception (and must not mark transaction rollback).
i think thats clever way of driver / persistence provider give chance repeat / fix statement instead of rolling whole transaction implicitly.
in general, think if had insert or update before find (or more complex data manipulation statements), pessimisticlockexception.
Comments
Post a Comment