2

Hibernate Optimistic ロックに関連する例外をキャッチする方法を知る必要があります。私の問題は、更新ステートメントを try/catch ブロックに入れても、例外がキャッチされないことです。サンプル コードは次のようになります。

このコードはサービス層に存在し、Spring MVC コントローラーを使用しています。この例外をキャッチするのに最適な場所はどれですか? それはコントローラーにありますか、それとも Service クラスにありますか? コントローラーでキャッチしようとしましたが、うまくいきませんでした。

私が感じているのは、Hibernate が独自の戦略で更新クエリを実行し、更新を実行するまでに、コントローラーから応答が既に送信されていることです。

それをキャッチして適切なメッセージをユーザーに表示する方法を提案してください。

try
{
//Some code to fetch the object from database
    Employee emp = employeeDao.load(id);
    emp.setName("xyz");
    employeeDao.saveOrUpdate(emp);
}
catch(StaleObjectStateException e)
{
    //TODO: Put the Exception in the response Object
    if (logger.isDebugEnabled()) logger.debug("Stale Data Exception: Request User to reload the Page");
    e.printStackTrace();
}

catch(HibernateOptimisticLockingFailureException e)
{
    //TODO: Put the Exception in the response Object
    if (logger.isDebugEnabled()) logger.debug("Stale Data Exception: Request User to reload the Page");
    e.printStackTrace();
}
catch(Exception e)
{
    //TODO: Put the Exception in the response Object
    if (logger.isDebugEnabled()) logger.debug("Stale Data Exception: Request User to reload the Page");
    e.printStackTrace();
}
4

0 に答える 0