title: How to capture Hibernate DB connection error exception?
body: <p>I am using Hibernate 3 in my development and WAS Liberty Profile as my local development server. I am trying to create an exception handling when DB server was down. To do this, I am trying simulate the environment by connecting to a fake IP address in <code>hibernate.cfg.xml</code> for this testing. </p>
  
  <p>I have the following JAVA code but the exception wasn't catch in the exception code when DB connection wasn't ready. The console output shows the connection could not be obtain as warning. May I know how could I capture my scenario in exception code instead of showing it as a warning?</p>
  
  <p><strong>JAVA code</strong></p>
  
  <pre><code>   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);
         }
       }
  
       ...
       ...
     }
  </code></pre>
  
  <p><strong>Console output</strong></p>
  
  <pre><code>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
  </code></pre>
  
  <p><strong>Hibernate configuration</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;
  &lt;hibernate-configuration&gt;
    &lt;session-factory&gt;
      &lt;property name="hibernate.connection.url"&gt;jdbc:db2://192.168.1.6:51201/MYDB&lt;/property&gt;
      &lt;property name="hibernate.connection.username"&gt;myuser&lt;/property&gt;
      &lt;property name="hibernate.connection.password"&gt;MyPassword&lt;/property&gt;
  
      &lt;property name="hibernate.connection.driver_class"&gt;com.ibm.db2.jcc.DB2Driver&lt;/property&gt;
      &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.DB2Dialect&lt;/property&gt;
      &lt;property name="hibernate.query.factory_class"&gt;org.hibernate.hql.classic.ClassicQueryTranslatorFactory&lt;/property&gt;
      &lt;property name="hibernate.jdbc.batch_size"&gt;100&lt;/property&gt;
      &lt;property name="hibernate.show_sql"&gt;false&lt;/property&gt;
  
      &lt;property name="hibernate.connection.release_mode"&gt;auto&lt;/property&gt;
      &lt;property name="current_session_context_class"&gt;thread&lt;/property&gt;
      &lt;property name="hibernate.connection.autocommit"&gt;true&lt;/property&gt;
      &lt;property name="hibernate.transaction.flush_before_completion"&gt;true&lt;/property&gt;
      &lt;property name="hibernate.transaction.auto_close_session"&gt;false&lt;/property&gt;
      &lt;property name="hibernate.cglib.use_reflection_optimizer"&gt;true&lt;/property&gt;
  
      ...
  
      &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>