以下是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");
}