我在我的开发和 WAS Liberty Profile 中使用 Hibernate 3 作为我的本地开发服务器。我正在尝试在数据库服务器关闭时创建异常处理。为此,我正在尝试通过连接hibernate.cfg.xml
到此测试的假 IP 地址来模拟环境。
我有以下 JAVA 代码,但是当数据库连接未准备好时,异常代码中没有捕获异常。控制台输出显示无法获取连接作为警告。我可以知道如何在异常代码中捕获我的场景而不是将其显示为警告吗?
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
休眠配置
<?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>