I'm having one issue that is how to catch OptimisticLockException in web layer (.war code) when it is raised by the EJB layer.
We are using JEE5, GlassFishV2.1 and JPA (with TopLinks)and Container Managed Transactions.But when the dirty read occur due to trnasaction by another concurrent user on the same entity.It gives Transaction.RollBackException at the war layer which is actually caused by OptimisticLockException.But i am unable to catch OptimisticLockException at war side.
that use of em.flush on ejb side and then we can catch and throw some custom exception to war. but I think em.flush will refresh all database, is it an expensive operation or not?
try{
//some enitity
em.flush()
}
catch(OptimisticLockException ole){
throw ole;
}
My opinion is not to call em.flush as 90% of cases there will be no OptimisticLockException and to catch the EJBException in the .war and then retry it. Is there a better option?
try{ // some code } catch (EJBException ex) {
if (ex.getCausedByException().getCause().toString().
indexOf("javax.transaction.RollbackException")!= -1){
// do work
}
}
}