I am using JPA toplink-essential when building a REST web application.
I have a servlet that finds one object and deletes it.
Below the code, I thought I could catch the optimistic blocking exception at the servlet level, but it doesn't! Instead, a RollbackException is thrown and that the documentation says:
But then, when I see the NetSE IDE GlassFish log, an optimisticLockException is thrown somewhere. It just doesn't get into my code. (My print message does not appear on the system, so I’m sure it won’t be there.)
I tried to import each package (one at a time, of course) and tested using catch, but both times, it is not included in the catch block, although a log error indicates an “optimistic exception”.
import javax.persistence.OptimisticLockException;
import oracle.toplink.essentials.exceptions.OptimisticLockException;
So where is the OptimisticLockException thrown
@Path("delete")
@DELETE
@Consumes("application/json")
public Object planDelete(String content) {
try {
EntityManager em = EmProvider.getInstance().getEntityManagerFactory().createEntityManager();
EntityTransaction txn = em.getTransaction();
txn.begin();
jObj = new JSONObject(content);
MyBeany bean = em.find(123);
bean.setVersion(Integer.parseInt(12345));
em.remove(bean);
em.getTransaction().commit();
}
catch(OptimisticLockException e) {
System.out.pritnln("here");
return HttpStatusHandler.sendConflict();
}
catch(RollbackException e) {
return HttpStatusHandler.sendConflict();
}
catch(Exception e) {
return HttpStatusHandler.sendServerError(e);
}
finally {
if(em != null) {
em.close();
}
}
Msg error:
[TopLink Warning]: 2011.01.28 05:11:24.007--UnitOfWork(22566987)
--Exception [TOPLINK-5006]
(Oracle TopLink Essentials - 2.0.1 (Build b09d-fcs (12/06/2007))):
oracle.toplink.essentials.exceptions.OptimisticLockException
[TopLink Warning]: 2011.02.01 08:50:15.095--UnitOfWork(681660)--
javax.persistence.OptimisticLockException: Exception [TOPLINK-5006] (Oracle TopLink
Essentials - 2.0.1 (Build b09d-fcs (12/06/2007))):
oracle.toplink.essentials.exceptions.OptimisticLockException
source
share