【发布时间】:2011-09-26 08:45:11
【问题描述】:
在 (REST) Web 服务中处理 OLE 的正确模式是什么?这就是我现在正在做的,例如,
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("optimistic lock exception, waiting to 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;
} finally {
em.close();
}
}
每当您在代码中看到休眠时,很有可能它是不正确的。有没有更好的方法来处理这个问题?
另一种方法是立即将故障发送回客户端,但我不想让他们担心。正确的做法似乎是尽一切努力使请求在服务器上成功,即使需要一段时间。
【问题讨论】: