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 hibernate.cfg.xml for this testing.
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?
JAVA 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);
}
}
...
...
}
Console output
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 configuration
<?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>
I think what you need is
test-on-connection-acquireSeems you are using the default Hibernate connection pool. Unfortunately, hibernate connection pool does not support his property and they recommend third party connection pool libraries like C3P0.
The official documentation of Hibernate discourages using their default connection.
Here is how you can configure C3P0 with hibernate. Here are relevant properties of C3P0 for testing connections.