views:

45

answers:

1

Unable to catch optimistic lock exception.

one way to raise OptimisticLockException is by using em.flush()

try{
   //some enitity
   em.flush()
  }
catch(OptimisticLockException ole){}

but i dont think this is best solution beacuse in this full database is flush.

another work around is by catching EJBException and find RollBackException in that ..

       try{
            // some code
        }
       catch (EJBException ex) {

          if (ex.getCausedByException().getCause().toString().
              indexOf("javax.transaction.RollbackException")!= -1){
                   // do work
              }     
          }
       }

Please help do you have any other idea or tell me which way is better.

A: 

I think the first way is a reasonable way if you want to catch OptimisticLockException and refresh your data or retry your operation. As for the second way, if the current transaction is not active, there is no RollbackException thrown.

Kevin Yang