-->
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.  [ 5 posts ] 
Author Message
 Post subject: Help Newbie to Hibernate: Cannot Open Connection ?
PostPosted: Tue Jan 11, 2005 9:19 pm 
Regular
Regular

Joined: Tue Jan 11, 2005 8:55 pm
Posts: 53
Hello,

I am new to Hibernate, and I am receiving an odd error in my sample application for saving a session. Specifically, here is the error: "Hibernate Exception: Cannot Open Connection".

I find this error very odd because I am able to establish a connection and successfully drop my db schema using the hbm2ddl utility tool. So, I have been able to successfully establish a connection (when exporting my database schema).

Plus, I am fairly positive my hibernate.properties file is fine; I do not have a hibernate.cfg.xml file. Furthermore, the mapping files, properties file are all included in the classpath. I've included below the the code portion, properties file, and the stack trace.

I appreciate any feedback or help; I have been stuck for hours. Thanks in advance for taking the time to helping me out..!

Here is the code:

Code:
Configuration cfg = null;
      SessionFactory sf = null;
      Session session = null;
      Properties props = new Properties();

      
      
      try{
         cfg = new Configuration()
                  .addClass(myDAO1.class)
                  .addClass(myDAO2.class)
                  .addClass(myDAO3.class);

         

         sf = cfg.buildSessionFactory();
         session = sf.openSession();

                  
         myDAO1 partner = new myDAO1();
myDAO1.setName("Joe");

         session.save(partner);
         
         session.flush();
         session.connection().commit();
      catch( MappingException me ){...   }
      }catch( HibernateException he ){ ...}
      }catch( SQLException se ){...}
         catch(HibernateException he1 ){...}catch( Exception e ){...}finally{
         try{
            if( session != null){
               session.close();
            }
               

         }catch( HibernateException he ){
            System.out.println( "HibernateException " + he.getMessage() );
         }
      }



Here is my Properties File
Code:
hibernate.dialect net.sf.hibernate.dialect.Oracle9Dialect
hibernate.dialect net.sf.hibernate.dialect.OracleDialect
hibernate.connection.driver_class oracle.jdbc.driver.OracleDriver
hibernate.connection.username user
hibernate.connection.password pwd
hibernate.connection.url jdbc:oracle:thin:@dev.aa.bb.cc.com:1234:sharedb
hibernate.show_sql=true



Here is the stack trace:

Code:
[java] WARNING: Could not obtain connection metadata
     [java] java.sql.SQLException: Io exception: The Network Adapter could not establish the connection
     [java] at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
     [java] at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:179)
     [java] at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:334)
.......
[java] WARNING: SQL Error: 17002, SQLState: null
     [java] Jan 11, 2005 4:51:04 PM net.sf.hibernate.util.JDBCExceptionReporter logExceptions
     [java] SEVERE: Io exception: The Network Adapter could not establish the connection
     [java] Hibernate Exception: Cannot open connection
     [java] Jan 11, 2005 4:51:25 PM net.sf.hibernate.util.JDBCExceptionReporter logExceptions
     [java] WARNING: SQL Error: 17002, SQLState: null
     [java] Jan 11, 2005 4:51:25 PM net.sf.hibernate.util.JDBCExceptionReporter logExceptions
     [java] SEVERE: Io exception: The Network Adapter could not establish the connection
     [java] Hibernate Exception: Cannot open connection


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 12, 2005 6:33 pm 
Regular
Regular

Joined: Tue Jun 22, 2004 8:01 pm
Posts: 106
Location: PowderTown, Utah, USA
Well, lemme read your stack trace for ya! :) It says that the adapter is unable to make the network connection. In my experience, exceptions like that one usually are telling you exactly what is happening: it can't make the network connection. So, check your JDBC string, check the network to make sure you can open a connection, check your ports, check that the database is running, and if all else fails, try to TELNET to the DB on the address and port. I find that helps trouble shoot network connection problems.

Not really sure this is related to Hibernate... might wanna ask your network admin for help though!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 13, 2005 1:40 pm 
Regular
Regular

Joined: Tue Jan 11, 2005 8:55 pm
Posts: 53
cardsharp wrote:
Well, lemme read your stack trace for ya! :) It says that the adapter is unable to make the network connection. In my experience, exceptions like that one usually are telling you exactly what is happening: it can't make the network connection. So, check your JDBC string, check the network to make sure you can open a connection, check your ports, check that the database is running, and if all else fails, try to TELNET to the DB on the address and port. I find that helps trouble shoot network connection problems.

Not really sure this is related to Hibernate... might wanna ask your network admin for help though!


Thanks for the feedback. I have a hunch as well it is a network related problem! I've checked all the above as you mentioned, but I'm positive they are all correct. Do you think it is any issue with how I am configuring my sessionfactory? Because I am able to establish a connection using the same port, db url, jdbc driver, etc when I use SchemaExport.

Is there a benefit of using a hibernate.cfg.xml file versus the hibernate.properties file?

Thanks again for taking the time to reply, much appreciated!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 13, 2005 6:23 pm 
Regular
Regular

Joined: Tue Jun 22, 2004 8:01 pm
Posts: 106
Location: PowderTown, Utah, USA
Interesting that SchemaExport is able to communicate with the DB and your app can't. There must be something different between the two... different url or port or something? Have you verified that you can telnet from that machine to the DB over the same port? That will at least rule out a network issue.

As far as the Hibernate.cfg.xml vs. the hibernate.properties, they both work. I've used them both, so it's a bit of a personal preference. I suspect (gurus correct me if I'm wrong) that the .properties version is historical and that the .xml version is newer. I'd go for the .xml since nearly everything is configured that way nowadays! But, like I said, I've got apps in production that use the .properties file and it works greak.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 14, 2005 2:43 am 
Regular
Regular

Joined: Tue Jan 11, 2005 8:55 pm
Posts: 53
cardsharp wrote:
Interesting that SchemaExport is able to communicate with the DB and your app can't. There must be something different between the two... different url or port or something? Have you verified that you can telnet from that machine to the DB over the same port? That will at least rule out a network issue.

As far as the Hibernate.cfg.xml vs. the hibernate.properties, they both work. I've used them both, so it's a bit of a personal preference. I suspect (gurus correct me if I'm wrong) that the .properties version is historical and that the .xml version is newer. I'd go for the .xml since nearly everything is configured that way nowadays! But, like I said, I've got apps in production that use the .properties file and it works greak.


Thanks cardsharp, it turned out to be a network issue. When I take the same code, and use a different database on another server the code works fine. I'll have a network admin now take care of the problem - I'm relieved to know that I wasn't doing anything wrong in my code.

Thanks again for your help! I appreciate it.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.