我使用Criteria API檢索hibernate中的對象列表。不過,我需要鎖定這些對象,因爲另一個同時執行的線程將獲得確切的對象,並且只有其中一個線程會在沒有悲觀鎖定的情況下成功。如何使用Criteria API指定悲觀鎖定?
我試過像下面,但它不工作。
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 ?
更新:似乎在3.5.2版本帕斯卡爾Thivent(非常感謝帕斯卡)中提到的錯誤,我已經加入爲成員和看問題。希望它會包含在下一個版本中。
但是我試着用session.buildLockRequest()
的另一種方法嘗試......但是我無法弄清楚如何使用它,並且使用下面的代碼根本沒有任何效果。
for (int i=0; i < n; i++)
session.buildLockRequest(LockOptions.UPGRADE).lock(esns.get(i));
「不工作」是什麼意思?沒有鎖的AFAIK兩次讀取都應該成功。 – 2010-06-12 11:50:40
@Péter,更新了我的問題。基本上我想在這個選擇後執行更新。 – Reddy 2010-06-12 11:57:20