LockMode in EJB3 Persistence NamedQuery

淺唱寂寞╮ 提交于 2020-01-25 09:10:08

问题


How do we specify LockMode in EJB3 Persistence NamedQuery? I want to add Pessimistic LockMode to my existing select so that I can update if necessary but surprisingly Query object doesnot have setLockMode(xxx) method ( My understanding was if JPA, asubset of EJB3 persistence, exposes setLockMode, EJB3 persistence should have the method available too).

Query query = em.createNamedQuery("findOptoutStudent");
query.setParameter("optoutIndicator", optoutIndicator);
List<Student> students = query.getResultList();
return students.get(0);

I would assume I dont have to change the query manually to "select for update".

Thanks Kevin


回答1:


Pessimistic Lock Mode :

  • PESSIMISTIC_READ which represents a shared lock. Lock request fails when another entity manager has acquired PESSIMISTIC_WRITE.

  • PESSIMISTIC_WRITE which represents an exclusive lock. Lock request fails when another entity manager has acquired either of the locks on the object.

Applying lock on the object

entityManager.lock(object, LockModeType.PESSIMISTIC_READ)

Releasing the lock afterwards

entityManager.lock(object, LockModeType.NONE)


来源:https://stackoverflow.com/questions/7839069/lockmode-in-ejb3-persistence-namedquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!