如何使用Criteria API指定悲观锁定? [英] How to specify pessimistic lock with Criteria API?

查看:170
本文介绍了如何使用Criteria API指定悲观锁定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Criteria API检索hibernate中的对象列表。然而,我需要锁定这些对象,因为另一个同时执行的线程将获得确切的对象,并且只有其中一个线程会成功,因为没有悲观的锁定。



 列出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)//完全不工作
.list();

更新:我在此声明后执行更新,以便我希望这两个线程读取不同的行,或者至少第二个线程应该等到第一个线程完成事务并离开锁。

以下是hibernate生成的查询。

  Hibernate:选择this_.id作为id1_0_,this_.name作为name1_0_,
this_.orderitem_id作为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 =?和this_.name =?通过this_.id命令限制asc的限制?

更新:在3.5.2版本中看起来像Pascal Thivent (非常感谢帕斯卡尔)提到,我作为成员加入并观看了该问题。我希望它会包含在下一个版本中。



然而,我尝试使用另一种方法,使用 session.buildLockRequest() ...但我无法弄清楚如何使用它,而使用下面的代码根本没有任何效果。

  for(int i = 0; i< n; i ++)
session.buildLockRequest(LockOptions.UPGRADE).lock(esns.get(i));


解决方案

您正在使用何种版本的Hibernate?这可以 HHH-5275 吗?你确定没有生成 FOR UPDATE 语句吗?你能显示生成的SQL吗?


I am retrieving a list of objects in hibernate using Criteria API. However I need lock on those objects as another thread executing at the same time will get the exact objects and only one of the thread will succeed in absence of a pessimistic lock.

I tried like below, but it is not working.

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: I am performing an update after this statement, so that I would like both threads to read different rows or at least second thread should wait till first thread completes with the transaction and leaves the lock.

And the hibernate generated query is below.

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 ?

Update: It seems a bug in 3.5.2 version as Pascal Thivent (Thanks a lot Pascal) mentioned, I have joined as member and watching the issue. Hopefully it will be included in the next release.

However I tried using another approach here with session.buildLockRequest()... but I couldn't quite figure out how to use it and using below code is not having any effect at all.

for (int i=0; i < n; i++)
    session.buildLockRequest(LockOptions.UPGRADE).lock(esns.get(i));

解决方案

What version of Hibernate are you using? Could this be HHH-5275? Are you sure that the FOR UPDATE statement isn't generated? Can you show the generated SQL?

这篇关于如何使用Criteria API指定悲观锁定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆