How to catch OptimisticLockException at servlet level?

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);


            //here commit!!!!!
            em.getTransaction().commit(); 
        }
        catch(OptimisticLockException e) {  //this is not caught here :(
            System.out.pritnln("here");
            //EntityTransactionManager.rollback(txn);
            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
+3
source share
3 answers

Not 100% sure, but it may be that you are catching javax.persistence.OptimisticLockException (pay attention to the package), but since oracle.toplink.essentials.exceptions.OptimisticLockException is the thrown exception, doesn’t it get caught? Although the name of the exception class is the same, they are not the same class.

+3
source

I would suggest that it is written out in the instructions em.getTransaction().commit();.

Because java doc from RollbackExceptio n if it says:

Issued by the continuity provider when EntityTransaction.commit () fails.

, , ( - ) bean.setVersion(Integer.parseInt(12345);), "", .

+2

entityManager.flush(); try/catch? JPA , OptimisticLock.

, . txn.commit(); em.getTransaction(). commit();.

, javax.persistence.OptimisticLockException. ReST SSB . SSB, -. flush() OLEX rethrows ApplicationException, Rest/SSB . , TransactionAttributeType.RequiresNew, , OLEX .

0

Source: https://habr.com/ru/post/1788423/


All Articles