如何捕获Hibernate数据库连接错误异常?

时间:2012-09-20 08:15:59

标签: java hibernate exception

我在我的开发中使用Hibernate 3,在我的本地开发服务器上使用WAS Liberty Profile。我试图在DB服务器关闭时创建异常处理。为此,我尝试通过连接到hibernate.cfg.xml中的虚假IP地址来模拟环境以进行此测试。

我有以下JAVA代码,但是当DB连接没有准备好时,异常代码中没有捕获异常。控制台输出显示无法获取连接作为警告。我是否可以知道如何在异常代码中捕获我的场景而不是将其显示为警告?

JAVA代码

   public class HibernateUtil { 
     static {
       try {
         log.info("HibernateUtil.static - Begin");

         if (cacheFactory.get("Factory") == null) {
           sessionFactory = new Configuration().configure().buildSessionFactory();
           cacheFactory.put("Factory", sessionFactory);
         } else {
           sessionFactory = (SessionFactory) cacheFactory.get("Factory");
         }

         log.info("HibernateUtil.static - end");

       } catch (HibernateException ex) {
         throw new RuntimeException("[Exception] : " + ex.getMessage(), ex);
       }
     }

     ...
     ...
   }

控制台输出

INFO  2012-09-20 15:24:16,314 [HibernateUtil.java:37] - HibernateUtil.static - Begin

[WARNING ] recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
[WARNING ] Property [hibernate.cglib.use_reflection_optimizer] has been renamed to [hibernate.bytecode.use_reflection_optimizer]; update your properties appropriately
[WARNING ] recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
[WARNING ] Property [hibernate.cglib.use_reflection_optimizer] has been renamed to [hibernate.bytecode.use_reflection_optimizer]; update your properties appropriately
[WARNING ] Could not obtain connection to query metadata

INFO  2012-09-20 15:24:24,001 [HibernateUtil.java:46] - HibernateUtil.static - end

Hibernate配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.connection.url">jdbc:db2://192.168.1.6:51201/MYDB</property>
    <property name="hibernate.connection.username">myuser</property>
    <property name="hibernate.connection.password">MyPassword</property>

    <property name="hibernate.connection.driver_class">com.ibm.db2.jcc.DB2Driver</property>
    <property name="hibernate.dialect">org.hibernate.dialect.DB2Dialect</property>
    <property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
    <property name="hibernate.jdbc.batch_size">100</property>
    <property name="hibernate.show_sql">false</property>

    <property name="hibernate.connection.release_mode">auto</property>
    <property name="current_session_context_class">thread</property>
    <property name="hibernate.connection.autocommit">true</property>
    <property name="hibernate.transaction.flush_before_completion">true</property>
    <property name="hibernate.transaction.auto_close_session">false</property>
    <property name="hibernate.cglib.use_reflection_optimizer">true</property>

    ...

    </session-factory>
</hibernate-configuration>

2 个答案:

答案 0 :(得分:2)

我认为您需要的是test-on-connection-acquire

似乎您正在使用默认的Hibernate连接池。不幸的是,hibernate连接池不支持他的属性,他们推荐第三方连接池库,如C3P0。

Hibernate的official documentation不鼓励使用默认连接。

  然而,

Hibernate自己的连接池算法非常简单   简陋。它旨在帮助您入门,而不是   打算用于生产系统,甚至用于性能   测试。您应该使用第三方池以获得最佳性能   稳定性。

Here is how您可以使用hibernate配置C3P0。用于测试连接的Here are relevant properties C3P0。

答案 1 :(得分:0)

什么类产生警告?我的猜测是你没有捕获异常,因为它没有被抛出。如果是这种情况,也许您可​​以测试是否在您的代码中建立了连接并自行抛出异常。