java - JPA: pattern for handling OptimisticLockException -


what correct pattern handling ole in (rest) web service? i'm doing now, example,

protected void dodelete(httpservletrequest request, httpservletresponse response)         throws servletexception, ioexception {      ...     ...     ...      try {         try {             em.gettransaction().begin();             // ... remove entity             em.gettransaction().commit();         } catch (rollbackexception e) {             if (e.getcause() instanceof optimisticlockexception) {                 try {                     clog.e("optimistic lock exception, waiting retry ...");                     thread.sleep(1000);                 } catch (interruptedexception ex) {                 }                 dodelete(request, response);                 return;             }         }          // ... write response      } catch (noresultexception e) {         response.senderror(httpservletresponse.sc_not_found, e.getmessage());         return;     } {         em.close();     } } 

anytime see sleep in code, there's chance it's incorrect. there better way handle this?

another approach send failure client, i'd rather not have them worry it. correct thing seems whatever required make request succeed on server, if takes while.

thanks.

if optimistic locking exception, means other transaction has committed changes entities trying update/delete. since other transaction has committed, retrying might have chance succeed.

i make method fail after n attempts, rather waiting stackoverflowexception happen.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -