title: JPA: pattern for handling OptimisticLockException
body: <p>What is the correct pattern for handling OLE in a (REST) web service? this is what I'm doing now, for example,</p>
  <pre><code>protected void doDelete(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
  
      ...
      ...
      ...
  
      try {
          try {
              em.getTransaction().begin();
              // ... remove the entity
              em.getTransaction().commit();
          } catch (RollbackException e) {
              if (e.getCause() instanceof OptimisticLockException) {
                  try {
                      CLog.e(&quot;optimistic lock exception, waiting to retry ...&quot;);
                      Thread.sleep(1000);
                  } catch (InterruptedException ex) {
                  }
                  doDelete(request, response);
                  return;
              }
          }
  
          // ... write response
  
      } catch (NoResultException e) {
          response.sendError(HttpServletResponse.SC_NOT_FOUND, e.getMessage());
          return;
      } finally {
          em.close();
      }
  }
  </code></pre>
  <p>anytime you see a sleep in the code, there's a good chance it's incorrect. Is there a better way to handle this?</p>
  <p>another approach would be to immediately send the failure back to the client, but I'd rather not have them worry about it. the correct thing seems to do whatever is required to make the request succeed on the server, even if it takes a while.</p>