4

I have some code that throws OptimisticLockException (JPA Exception) :

try{
    account.someFunction();
}catch(OptimisticLockException ole){
    logger.log(Level.DEBUG, "OptimisticLockException with ole !");      
}catch(Exception e){
    logger.log(Level.DEBUG, "OptimisticLockException with e !");
}

And I received always OptimisticLockException with e !. My server logs display :

Exception [EclipseLink-5006] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.OptimisticLockException
...

My OptimisticLockException in my catch block is of type org.eclipse.persistence.exceptions.OptimisticLockException like in my server logs but I never fall in this catch block...

Why...?

Thanks for ideas.

PS: I work with Maven and Glassfish. At beginning, I worked with artifact eclipselink v2.4.1 whereas server side is 2.3.2 (see logs) so I change Maven version to 2.3.2 but it's the same result.

Olivier J.
  • 3,115
  • 11
  • 48
  • 71
  • try to catch the jpa exception, i think some code in eclipselink is catching the org.eclipse.persistence exception and rethrowing it as the "caused by" for a new javax.persistence exception. – guido Nov 25 '12 at 00:10
  • It is but JPA OptimisticLockException is wrapped inside RollbackException too and RollbackException is wrapped inside EJBException finally in my case – Olivier J. Nov 26 '12 at 14:16
  • You are wrapping you JPA code in wrappers that are required to throw their own exceptions. Most likely because of transaction handling occurring at the end of the method. You can try calling em.flush which should allow the optimistic lock to be handled within your ejb. – Chris Nov 26 '12 at 14:36
  • Nice, I saw this to a forum but I forgot this. Thanks to em.flush() my code is a bit more structured but is there any consequence on performance ? Ty – Olivier J. Nov 26 '12 at 14:50

2 Answers2

1

Apparently, we can't catch OptimisticLockException (either in javax.persistence or in org.eclipse.persistence.exceptions packages) directly because it is wrapped inside others Exceptions.

I tested this code :

try{
        account.someFunction();
    }catch(OptimisticLockException o1){
        logger.log(Level.DEBUG, "org.eclipse.persistence.exceptions.OptimisticLockException thrown !");         
    }catch(javax.persistence.OptimisticLockException o2){
        logger.log(Level.DEBUG, "javax.persistence.OptimisticLockException thorwn !");          
    }catch(Exception e){            

        int i=1;

        for(Throwable t=(Throwable)e; t!=null; t=t.getCause()){
            if(t instanceof OptimisticLockException)
                logger.log(Level.DEBUG, "org.eclipse.persistence.exceptions.OptimisticLockException thrown by Exception block ! " + i);
            else if(t instanceof javax.persistence.OptimisticLockException)
                logger.log(Level.DEBUG, "javax.persistence.OptimisticLockException thrown by Exception block ! " + i);
            else
                logger.log(Level.DEBUG, t + " " + i);
            i++;
        }

    }

I get :

2012-11-26 14:58:03,515 FATAL [com.sim.web.LoginBean] javax.ejb.EJBException: Transaction aborted 1

2012-11-26 14:58:03,515 FATAL [com.sim.web.LoginBean] javax.transaction.RollbackException: Transaction marked for rollback. 2

2012-11-26 14:58:03,515 FATAL [com.sim.web.LoginBean] javax.persistence.OptimisticLockException thrown by Exception block ! 3

2012-11-26 14:58:03,515 FATAL [com.sim.web.LoginBean] org.eclipse.persistence.exceptions.OptimisticLockException thrown by Exception block ! 4

So there is first a org.eclipse.persistence.exceptions.OptimisticLockException which is wrapped into javax.persistence.OptimisticLockException which is wrapped into javax.transaction.RollbackException which is wrapped into javax.ejb.EJBException (I'm working with EJBs and JTA).

I can't test with catch block RollbackException exception because Eclipse doesn't detect such exception can be thrown : Unreachable catch block for RollbackException. This exception is never thrown from the try statement body.

It's ugly but I think I'm condemned to catch exceptions of type Exception and recursively test if it's contain exception of type OptimisticLockException...

Thank you for previous advices

Olivier J.
  • 3,115
  • 11
  • 48
  • 71
  • look here (second answer): http://stackoverflow.com/questions/4838786/hibernate-jpa-how-to-handle-staleobjectstateexception-when-several-object-has – guido Nov 26 '12 at 22:10
0

Try changing your code to the following:

try {
    account.someFunction();
}
catch(javax.persistence.OptimisticLockException ole) {
    logger.log(Level.DEBUG, "OptimisticLockException with ole !");      
}
catch(Exception e) {
    logger.log(Level.DEBUG, "OptimisticLockException with e !");
}

And see if that works. I would think you would want to catch the JPA OptimisticLockException, not an Eclipse implementation.

Also, double-check your logging configuration, make sure your threshold is correct and whatnot, see if you are able to get other log statements at DEBUG level (perhaps put a log statement at DEBUG level prior to account.someFunction();.

Philip Tenn
  • 6,003
  • 8
  • 48
  • 85