如何使用 Criteria API 指定悲观锁?

发布于 2024-09-05 13:48:27 字数 1214 浏览 2 评论 0原文

我正在使用 Criteria 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();

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

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 Thivent(非常感谢Pascal)提到的,这似乎是3.5.2版本中的一个错误,我已加入为会员并关注该问题。希望它将包含在下一个版本中。

不过,我尝试在这里使用另一种方法 session.buildLockRequest()...但我不太清楚如何使用它,并且使用下面的代码根本没有任何效果。

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

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));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离笑几人歌 2024-09-12 13:48:27

您使用什么版本的 Hibernate?这可能是 HHH-5275 吗?您确定没有生成 FOR UPDATE 语句吗?你能显示生成的 SQL 吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文