3

我有这段代码:

        } catch (HibernateException e) {

        loginAnswer = new LoginCustomerAreaAnswer(999);

        //This function use the error code save inside loginAnswer
        this.logOp.error(logsUtilities.logException(e, "HibernateException"));


    } catch (Exception e) {

        loginAnswer = new LoginCustomerAreaAnswer(997);

        //This function use the error code save inside loginAnswer
        this.logOp.error(logsUtilities.logException(e, "Exception"));


    } finally {
        return loginAnswer;
    }

如您所见,我首先捕获 HibernateException 类型异常,然后捕获泛型异常。

但是当我查看日志文件时,当我有一个 org.hibernate.exception.GenericJDBCException 异常时,它就像一个通用异常一样被捕获!

为什么? GenericJDBCException 它不是 HibernateException 的“儿子”?不必被 HibernateException 捕获?

这是我的日志文件的示例

2013-05-21 11:01:02 [Level: ERROR]*** Exception: Error code: 997 - Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.GenericJDBCException: Cannot open connection

我真的迷路了,有人可以帮助我吗?

4

2 回答 2

3

很可能您遇到了 Spring 异常:

org.springframework.transaction.CannotCreateTransactionException:无法为事务打开休眠会话;嵌套异常是 org.hibernate.exception.GenericJDBCException:无法打开连接

于 2013-05-21T19:40:27.220 回答
0

我相信问题出在你的构造函数上:

new LoginCustomerAreaAnswer(...)

传递值 997。

运行此代码:

public class Test
{
    public static void main( String[] args )
    {
        try
        {
            throw new GenericJDBCException( "I'm dead", new SQLException() );
        } catch ( HibernateException hex )
        {
            System.err.println( "HibernateException" );
            hex.printStackTrace();
        } catch ( Exception ex )
        {
            System.err.println( "Exception" );
            ex.printStackTrace();
        }
    }
}

正如预期的那样,给了我HibernateException:

HibernateException
org.hibernate.exception.GenericJDBCException: I'm dead
    at com.execon.utils.Test.main(Test.java:19)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.sql.SQLException
    ... 6 more
于 2013-05-21T19:40:08.613 回答