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