以下是EJB的兩個function,我用 client 產生兩個thread分別呼叫
EJB 的 test1() 和 test2():
照理來說,test1()中把 u lock 起來後,睡了十秒的期間執行了 test2()
而 test2() 理論上想要把同一筆資料再鎖起來時應該要發生
LockTimeoutException 才對啊?
可是我試的結果卻是 test2() 會停在那等十秒過了,然後 test1() commit
接著再從 after lock2 做下去然後 commit?
請問有高手用過 PESSIMISTIC_WRITE 嗎?
另外設定了 javax.persistence.lock.timeout 好像也沒屁用 @@
不知道是不是我用錯了什麼?
client 端:
Thread t1: ejb.test1()
Thread t2: ejb.test2()
呼叫程序:
t1.start();
Thread.sleep(1000) ;
t2.start();
@Override
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void test1() {
User u = em.find(User.class, 1);
System.out.println("lock1");
em.lock(u, LockModeType.PESSIMISTIC_WRITE);
System.out.println("after lock1");
sleep(10000) ;
u.setOrders("X");
System.out.println("update X");
}
@Override
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void test2() {
User u = em.find(User.class, 1);
Map<String,Object> p = new HashMap<String,Object>();
p.put("javax.persistence.lock.timeout", 1000);
System.out.println("lock2");
em.lock(u, LockModeType.PESSIMISTIC_WRITE,p);
System.out.println("after lock2");
u.setOrders("Y");
System.out.println("update Y");
}