When trying to setup a lock (pessimistic), with the following code:
em.lock(controlnumbers, LockModeType.WRITE);
em.refresh(controlnumbers);
I am getting the following exception:
[#|2009-09-10T15:42:48.324-0400|INFO|sun-appserver2.1|javax.enterprise.system.container.ejb|_ThreadID=31;_ThreadName=httpSSLWorkerThread-8080-19;|
javax.ejb.TransactionRolledbackLocalException: Exception thrown from bean; nested exception is: javax.persistence.PersistenceException: ejb30-wrong-lock_called_without_version_locking-index (There is no English translation for this message.)
javax.persistence.PersistenceException: ejb30-wrong-lock_called_without_version_locking-index (There is no English translation for this message.)
at oracle.toplink.essentials.internal.ejb.cmp3.base.EntityManagerImpl.lock(EntityManagerImpl.java:619)
at com.sun.enterprise.util.EntityManagerWrapper.lock(EntityManagerWrapper.java:582)
at com.eximtechnologies.transactionserver.persistence.session.ControlNumbersFacade.lock(ControlNumbersFacade.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
How can I implement pessimistic locking with Glassfish 2.1?
There is a Toplink Essentials (GF 2.1 default) specific way to do this:
public MyObject lock (MyObject controlnumbers) {
String qStr = "select object(o) from MyObject as o where o.pk = :pk";
Query q = em.createQuery(qStr);
q.setParameter("pk", "a");
q.setHint("toplink.pessimistic-lock", "Lock");
controlnumbers = (MyObject)q.getSingleResult();
return controlnumbers;
}
I believe with Hibernate calling em.lock will actually work.