Hitachi

uCosminexus Application Server Compatibility Guide


9.10.2 Exception processing when optimistic lock fails

If you want to check the execution of an optimistic lock explicitly, invoke the flush() method. If an optimistic lock exception occurs during the invocation of the flush() method, you can catch the exception processing and perform the recovery processing. If the optimistic lock has no problems, the entity version is checked and the Version column is updated by the flush() method.

A coding example of the exception processing is as follows:

try{
  em.flush();
} catch (OptimisticLockException e){
  //  Exception processing
}

As shown in this example, by wrapping OptimisticLockException during the exception processing, you can show the optimistic lock exception as an application exception. However, note that the transaction in this case is marked for rollback and cannot be committed.

Note that with Cosminexus JPA Provider, the entity that causes the exception is not stored in OptimisticLockException. The getEntity() method of OptimisticLockException always returns a null value.