Hello. If I've missed the answer to this I apologize. I'd appreciate any nudge in the right direction.
Short question: Can I get the database URL given only a Session object?
Long version: I'm writing an Oracle database monitoring system using Hibernate. I iterate through a list of SessionFactory's which represent connections to the databases I want to monitor. I then open a Session and pass it to a Database test class to perform some sort of check. The test class knows only the session it's been given. On most tests, if the database connection fails, I just log it and move on; but I'm writing a CheckConnect test which should alert me that the connection failed. I'd like the alert to include the database URL. The session factory is configured using a hibernate.cfg.xml file. I've tried using envirnment.getProperties(), but the hibernate.connection.url property doesn't appear to be defined. I guess that's the result of using an xml confiuration file instead of hibernate.properties.
I'm using Hibernate version 2.1.3 and Oracle 8.1.7.4
Here is a snippet of a simplified example:
Code:
File f = new File(
"c:/Documents and Settings/JARRELLM/hibernate.cfg.xml");
try {
SessionFactory sessionFactory = new Configuration().configure(f)
.buildSessionFactory();
Session session = sessionFactory.openSession();
Properties props = Environment.getProperties();
for (Enumeration en = props.keys(); en.hasMoreElements();) {
String key = (String)en.nextElement();
lgr.info(key + " = " + props.getProperty(key));
}
lgr.debug("Here's a list of Dual stuff");
List dual = session.find("from ConnectData");
for (Iterator it = dual.iterator(); it.hasNext();) {
ConnectData cd = (ConnectData) it.next();
lgr.info(cd.getId());
}
session.close();
} catch (HibernateException e) {
Here is the hibernate.cfg.xml:
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">net.sf.hibernate.dialect.OracleDialect</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="show_sql">false</property>
<property name="connection.url">jdbc:oracle:thin:@server:1521:sid</property>
<property name="connection.username">system</property>
<property name="connection.password">password</property>
<property name="connection.pool_size">2</property>
<!-- Mapping files -->
<mapping resource="com/fhr/cc/dba/ConnectData.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Here is the mapping document:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping package="com.fhr.cc.dba">
<class name="ConnectData" table="dual">
<id name="id" type="string" unsaved-value="null" >
<column name="dummy" sql-type="varchar" length="1" not-null="true"/>
<generator class="assigned"/>
</id>
</class>
</hibernate-mapping>
The only Hibernate property listed in the output is hibernate.cglib.use_reflection_optimizer = true.
Thanks again for any help you can provide,
Maury