Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java异常似乎在被捕获后重新启动_Java_Hibernate_Exception - Fatal编程技术网

Java异常似乎在被捕获后重新启动

Java异常似乎在被捕获后重新启动,java,hibernate,exception,Java,Hibernate,Exception,我遇到了一个我不明白的情况。我有以下web服务(使用下面的一组伪代码) 在我的事务管理器类中: Response HandleRequest(List<Object> myStuff) throws RemoteException { Map<Object, String> problemMap = dataManager.saveMyStuff(myStuff) //Nothing here gets executed because Hibe

我遇到了一个我不明白的情况。我有以下web服务(使用下面的一组伪代码)

在我的事务管理器类中:

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;
    }
}
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;
}
Response HandleRequest(List myStuff)引发RemoteException
{ 
Map problemMap=dataManager.saveMyStuff(myStuff)
//这里没有执行任何操作,因为HibernateSystemExeption被神秘地(重新)抛出
日志警告(“步骤5”);
如果(problemMap.size()>0)
{
//将问题对象和错误消息放入响应中
返回响应;
}
}
在我的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;
}
Map saveMyStuff(列出mystuff)
{
Session sess=getSession();
事务tx=sess.beginTransaction();
布尔成功=真;
映射问题=新的HashMap();
for(对象o:mystuff)
{
尝试
{
//在这里执行一些hibernate操作,抛出HibernateSystemException
}
捕获(例外e)
{
log.warn(“捕获了一个例外!”,e);
成功=错误;
日志警告(“步骤1”);
问题。放置(o,“无法存储对象”);
日志警告(“步骤2”);
}
}
日志警告(“步骤3”);
尝试
{
如果(成功)
tx.commit;
其他的
tx.回滚();
}
捕获(例外e)
{
log.warn(“捕获到另一个异常!”,e);
}
日志警告(“步骤4”);
退货问题;
}
发生的情况是在datamanager中成功捕获了异常,我在日志文件中看到了它。代码继续执行return语句。但是,当它从datamanager返回时,我在事务管理器中得到相同的异常,就好像datamanager从未捕捉到它一样,并且该异常作为远程异常而不是响应传递回用户raw。调用saveMyStuff后,事务管理器中没有任何内容。这就好像异常保留在堆栈上,直到方法返回,然后再次抛出,即使它已在datamanager中处理。以前有没有人见过这样的事情,或者知道会发生什么

编辑:经过一段时间的实验,问题似乎与它是HibernateSystemException这一事实有关。如果我抛出一个泛型异常作为测试,那么代码就会像我预期的那样工作。那么HibernateSystemException是什么导致了这种奇怪的行为呢


编辑2:我已经按照建议完成了,并将事务提交/回滚包装在try/catch中。异常仍然会返回给用户。我还添加了调试打印语句。在日志中,我看到打印的是步骤1-4,而不是步骤5。我也没有看到从事务提交/回滚块捕获异常。

您描述的与Java异常处理逻辑不匹配。因此,发生了一些不同的情况,例如,tx.rollback()可能会引发异常。尝试在Try-catch中包含回滚,并在那里处理异常。

我以前的答案不正确,因此出现了新代码。我建议使用另一个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;
}
Map saveMyStuff(列出mystuff)
{
Session sess=getSession();
事务tx=sess.beginTransaction();
布尔成功=真;
映射问题=新的HashMap();
for(对象o:mystuff)
{
尝试
{
//在这里执行一些hibernate操作,抛出HibernateSystemException
}
捕获(例外e)
{
log.warn(“捕获了一个例外!”,e);
成功=错误;
问题。放置(o,“无法存储对象”);
}
}
尝试
{
如果(成功)
tx.commit;
其他的
tx.回滚();
}
捕获(例外e)
{
log.warn(“捕获了一个例外!”,e);
问题。放置(o,“无法存储对象”);
}
退货问题;
}

好的,我发现了问题。因为我调用的是getSession而不是openSession,所以当方法返回时,会话被隐式刷新。刷新会话的行为导致引发第二个异常。我通过在返回之前显式刷新会话并在try/catch中围绕会话来确认这一点。它捕获了异常,但在返回时再次刷新会话时仍会抛出该异常。最终的修复方法是调用openSession,并用try-catch块将会话刷新

DataManager类:

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;
    }
}
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;
}
Map saveMyStuff(列出mystuff)
{
Session sess=getSessionFactory.openSession();
事务tx=sess.beginTransaction();
布尔成功=真;
映射问题=新的HashMap();
for(对象o:mystuff)
{
尝试
{
//在这里执行一些hibernate操作,抛出HibernateSystemException
}
捕获(例外e)
{
log.warn(“捕获了一个例外!”,e);
成功=错误;
问题。放置(o,“无法存储对象”);
}
}
尝试
{
如果(成功)
tx.commit;
其他的
tx.回滚();
sess.flush();
}
捕获(例外e)
{
log.warn(“捕获到另一个异常!”,e);
}
最后
{
sess.close();
}
退货问题;
}

谢谢大家的帮助

你能发布异常的stacktrace并在代码中添加一个注释,告诉我们它发生在哪一行吗?还有:你在使用spring吗?有注释还是什么?你能试着添加一个finally,do a session.close()@SimoneGianni,异常最初会在DataManager中注释所在的代码中抛出。对于这个问题,异常的内容并不重要,因为我是故意导致测试异常的。如果有帮助的话,这是带注释的Spring。您认为的例外可能是percolatin