我正在使用条件API检索hibernate中的对象列表。但是,我需要锁定这些对象,因为同时执行的另一个线程将得到确切的对象,并且在没有悲观锁的情况下,只有一个线程会成功。
我试过像下面这样做,但不起作用。
List esns = session
.createCriteria(Reddy_Pool.class)
.add(Restrictions.eq("status", "AVAILABLE"))
.add(Restrictions.eq("name", "REDDY2"))
.addOrder(Order.asc("id"))
.setMaxResults(n)
.setLockMode(LockMode.PESSIMISTIC_WRITE) //not working at all
.list();update :我在这个语句之后执行一个更新,所以我希望两个线程读取不同的行,或者至少第二个线程应该等到第一个线程与事务一起完成并离开锁。
以下是hibernate生成的查询。
Hibernate: select this_.id as id1_0_, this_.name as name1_0_,
this_.orderitem_id as orderitem3_1_0_, this_.status as status1_0_,
this_.store as store1_0_, this_.vendor as vendor1_0_, this_.version as version1_0_
from reddy_pool this_
where this_.status=? and and this_.name=? order by this_.id asc limit ?更新:正如Pascal (非常感谢Pascal)所提到的,它似乎是3.5.2版本中的一个bug,我以成员身份加入并关注这个问题。希望它将包含在下一个版本中。
然而,我尝试用另一种方法在这里使用session.buildLockRequest()..。但我不太清楚如何使用它,使用下面的代码根本没有任何效果。
for (int i=0; i < n; i++)
session.buildLockRequest(LockOptions.UPGRADE).lock(esns.get(i));发布于 2010-06-12 12:11:04
您使用的是什么版本的Hibernate?这可能是HHH-5275吗?您确定没有生成FOR UPDATE语句吗?您能显示生成的SQL吗?
https://stackoverflow.com/questions/3028478
复制相似问题