how to raise OptimisticLockException
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
}
}
}
Pleas开发者_JS百科e help do you have any other idea or tell me which way is better.
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.
try
{
getEntityRepository().update("Some Persistence Obj");
}
catch (EJBException e)
{
if (e.getCause() instanceof OptimisticLockException)
{
// code goes here
}
}
精彩评论