本文介绍了EJB3持久性NamedQuery中的LockMode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何在EJB3持久性NamedQuery中指定LockMode?我想将Pessimistic LockMode添加到我现有的选择中,以便我可以在必要时进行更新,但是令人惊讶的是,查询对象没有setLockMode(xxx)方法(我的理解是,如果JPA是EJB3持久性的子集,则公开setLockMode,EJB3持久性应该具有可用的方法也).

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".

谢谢凯文

推荐答案

悲观锁定模式:

  • PESSIMISTIC_READ表示共享锁.当另一个实体管理器获取PESSIMISTIC_WRITE时,锁定请求失败.

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

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.

在对象上应用锁定

entityManager.lock(object, LockModeType.PESSIMISTIC_READ)

随后释放锁

entityManager.lock(object, LockModeType.NONE)

这篇关于EJB3持久性NamedQuery中的LockMode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-05 05:39