-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Database URL from Session?
PostPosted: Mon Jul 26, 2004 5:15 pm 
Beginner
Beginner

Joined: Mon Jul 26, 2004 4:29 pm
Posts: 45
Location: TX, USA
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


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 26, 2004 7:49 pm 
Beginner
Beginner

Joined: Wed Jul 21, 2004 12:28 pm
Posts: 27
Location: New York
Well, not really - as it appears all of that is external. So some digging inside hibernate will be required. My guess is that you can do the following:

((SessionFactoryImpl) session
.getSessionFacotry ())
.getConnectionProvider ()

This will get you connection provider that you can specify when you configure hibernate. Just overwrite one of the exiting providers, the one that you are currently using, and add an extra method to retrieve your needed property. Then cast the above statement to the connection provider you use and call the extra method.

HTH,
Alex

_________________
Thanks,
Alex


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 27, 2004 8:36 am 
Beginner
Beginner

Joined: Mon Jul 26, 2004 4:29 pm
Posts: 45
Location: TX, USA
Thanks. I'll try that and post my results.

Cheers,
Maury


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 29, 2004 11:45 am 
Beginner
Beginner

Joined: Mon Jul 26, 2004 4:29 pm
Posts: 45
Location: TX, USA
Results: I actually went a slightly different route, but one that I probably should have done all along. Instead of one list of tests and another list of SessionFactories, I create a list of database tests with one of each test per database. The SessionFactory and the Database Name can then be embedded in each test and are available to open Sessions, or, in the case of the Database Name, provide needed info when the connection fails.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.