捕获HibernateOptimisticLockingFailureException异常

时间:2013-08-05 07:14:37

标签: hibernate

我需要知道如何捕获与Hibernate Optimistic锁相关的异常。 我的问题是异常是没有被捕获,即使我已将更新语句放在try / catch块中。我的示例代码看起来像这样。

此代码存在于服务层中,我使用的是Spring MVC控制器。哪个是捕获此异常的最佳位置?是在控制器中还是在服务类中? 我试着在控制器中捕捉它,即使这不起作用。

我觉得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();
}

0 个答案:

没有答案