我遇到了一个我不明白的场景。我有以下web服务(使用下面的一堆伪代码)。
在我的transaction Manager类中:
Response HandleRequest(List<Object> myStuff) throws RemoteException
{
Map<Object, String> problemMap = dataManager.saveMyStuff(myStuff)
//Nothing here gets executed because HibernateSystemExeption was mysteriously (re)thrown
log.warn("Step 5");
if(problemMap.size() > 0)
{
//Put problem objects and error messages in response
return response;
}
}然后在我的DataManager类中,我有:
Map<Object, String> saveMyStuff(List<Object> mystuff)
{
Session sess = getSession();
Transaction tx = sess.beginTransaction();
boolean successful = true;
Map<Object, String> problems = new HashMap<Object, String>();
for(Object o : mystuff)
{
try
{
//Do some hibernate stuff here that throws a HibernateSystemException
}
catch(Exception e)
{
log.warn("Caught an exeption!", e);
successful = false;
log.warn("Step 1");
problems.put(o, "Couldn't Store object");
log.warn("Step 2");
}
}
log.warn("Step 3");
try
{
if(successful)
tx.commit;
else
tx.rollback();
}
catch(Exception e)
{
log.warn("Another Exception caught!", e);
}
log.warn("Step 4");
return problems;
}发生的情况是,在数据管理器中成功捕获了异常,我在日志文件中看到了它。代码继续执行return语句。但是,当它从数据管理器返回时,我在事务管理器中得到相同的异常,就好像它从未被数据管理器捕获一样,并且该异常被作为远程异常而不是响应传递回用户raw。在执行对saveMyStuff的调用之后,事务管理器中没有任何内容。这就好像异常一直保留在堆栈上,直到方法返回,然后再次抛出,即使它已经在数据管理器中处理过了。有没有人以前见过这样的事情,或者知道可能会发生什么?
编辑:经过更多的实验,这个问题似乎与它是一个HibernateSystemException的事实有关。如果我抛出一个泛型异常作为测试,代码就会像我预期的那样工作。那么是什么关于HibernateSystemException导致了这种奇怪的行为呢?
编辑2:我已经按照建议做了,并将事务提交/回滚包装在try/catch中。异常仍然会返回给用户。我还添加了debug print语句。在日志中,我看到打印了步骤1-4,但没有打印5步。我也没有看到从事务提交/回滚块捕获到异常。
发布于 2014-09-18 03:12:38
您所描述的与Java异常处理逻辑不匹配。因此,一些不同的事情正在发生,例如,tx.rollback()可能会抛出异常。尝试在try-catch中包含回滚,并在那里处理异常。
发布于 2014-09-18 03:15:25
我之前的答案是不正确的,所以有了新的代码。我建议使用另一个try catch块,以确保不会抛出第二个异常。
Map<Object, String> saveMyStuff(List<Object> mystuff)
{
Session sess = getSession();
Transaction tx = sess.beginTransaction();
boolean successful = true;
Map<Object, String> problems = new HashMap<Object, String>();
for(Object o : mystuff)
{
try
{
//Do some hibernate stuff here that throws a HibernateSystemException
}
catch(Exception e)
{
log.warn("Caught an exeption!", e);
successful = false;
problems.put(o, "Couldn't Store object");
}
}
try
{
if(successful)
tx.commit;
else
tx.rollback();
}
catch(Exception e)
{
log.warn("Caught an exeption!", e);
problems.put(o, "Couldn't Store object");
}
return problems;
}发布于 2014-09-18 03:43:16
好吧,我找到问题了。因为我调用的是getSession而不是openSession,所以当方法返回时,会话将被隐式刷新。刷新会话的操作导致引发第二个异常。我通过在返回之前显式刷新会话并将其包含在try/catch中来确认这一点。这捕获了异常,但在返回时再次刷新会话时仍会抛出异常。最终的修复方法是调用openSession并使用try catch块围绕会话刷新。
DataManager类:
Map<Object, String> saveMyStuff(List<Object> mystuff)
{
Session sess = getSessionFactory.openSession();
Transaction tx = sess.beginTransaction();
boolean successful = true;
Map<Object, String> problems = new HashMap<Object, String>();
for(Object o : mystuff)
{
try
{
//Do some hibernate stuff here that throws a HibernateSystemException
}
catch(Exception e)
{
log.warn("Caught an exeption!", e);
successful = false;
problems.put(o, "Couldn't Store object");
}
}
try
{
if(successful)
tx.commit;
else
tx.rollback();
sess.flush();
}
catch(Exception e)
{
log.warn("Another Exception caught!", e);
}
finally
{
sess.close();
}
return problems;
}感谢大家的帮助!
https://stackoverflow.com/questions/25897677
复制相似问题