GenericJDBCException被try / catch块的错误部分捕获

时间:2013-05-21 19:18:18

标签: java hibernate exception

我有这段代码:

        } 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异常时,它就像一个普通的Exception一样被捕获!

为什么?,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

我真的迷失了,有人能帮助我吗?

2 个答案:

答案 0 :(得分:3)

很可能你遇到了Spring Exception:

org.springframework.transaction.CannotCreateTransactionException:无法打开Hibernate Session进行事务处理;嵌套异常是org.hibernate.exception.GenericJDBCException:无法打开连接

答案 1 :(得分: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