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: Catching connection error?
PostPosted: Wed Dec 27, 2006 1:10 pm 
Newbie

Joined: Wed Dec 27, 2006 12:36 pm
Posts: 3
Hibernate version:
3.2.1

Name and version of the database you are using:
MySQL 4.x


I'm relatively new to Hibernate, so I was hoping to get some help on this...it should be easy but I just can't figure it out!

So here's the problem: I am not able to detect if the database connection settings are incorrect until I try to interact with the database (save, query, delete, etc.). I would like to be able to tell when I first open the Session. The odd thing is that when I open the Session, an exception (SQLException) is logged from the org.hibernate.cfg.SettingsFactory object with the text 'Could not obtain connection metadata' but it is consumed and handled before it gets to my code.

I am having this problem with both an incorrect IP address (SQLState error 08S01) and incorrect login information with the correct database IP. For an incorrect IP address, I can't handle the error until the database connection times out twice (one for the Session creation, one for a query) which is a long time to wait.

Background: I am setting up the connection manually by creating a Configuration object and setting up the properties, then I add the mapping file to the Configuration object (addResource), then I build the SessionFactory, and then use the factory to open the Session. It works fine when I have the correct connection settings defined.

Any help would be greatly appreciated!!!


Top
 Profile  
 
 Post subject: Re: Catching connection error?
PostPosted: Wed Dec 27, 2006 1:52 pm 
Senior
Senior

Joined: Sun Jun 04, 2006 1:58 am
Posts: 136
gsegelk wrote:
Hibernate version:
3.2.1

Name and version of the database you are using:
MySQL 4.x


I'm relatively new to Hibernate, so I was hoping to get some help on this...it should be easy but I just can't figure it out!

So here's the problem: I am not able to detect if the database connection settings are incorrect until I try to interact with the database (save, query, delete, etc.). I would like to be able to tell when I first open the Session. The odd thing is that when I open the Session, an exception (SQLException) is logged from the org.hibernate.cfg.SettingsFactory object with the text 'Could not obtain connection metadata' but it is consumed and handled before it gets to my code.

I am having this problem with both an incorrect IP address (SQLState error 08S01) and incorrect login information with the correct database IP. For an incorrect IP address, I can't handle the error until the database connection times out twice (one for the Session creation, one for a query) which is a long time to wait.

Background: I am setting up the connection manually by creating a Configuration object and setting up the properties, then I add the mapping file to the Configuration object (addResource), then I build the SessionFactory, and then use the factory to open the Session. It works fine when I have the correct connection settings defined.

Any help would be greatly appreciated!!!


why can't you do the check yourself before creating the session factory

http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Connection.html#getMetaData()

_________________
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 27, 2006 4:10 pm 
Newbie

Joined: Wed Dec 27, 2006 12:36 pm
Posts: 3
I think what you are saying will solve my problem, but I don't know how to get access to the Connection object (for some reason, I feel really dumb saying that...it will probably hit me like a ton of bricks once I see what's going on!)

I currently go through the following steps before getting a Session object via factory.openSession():
1. Create a Configuration object
2. Use the 'setProperty' method on the Configuration object for db IP, port, user, password
3. Use the 'addResource' method on the Configuration object to set the mapping file
4. Create a SessionFactory using the 'buildSessionFactory' method on the Connection object
5. Create the session using the 'openSession' method on the SessionFactory object
6. Interact with the Session object (.save(), etc)

Is there a different way I should be initiating the connection manually? Thank you for your quick response and it seems like the issue is close to being solved!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 27, 2006 4:36 pm 
Senior
Senior

Joined: Sun Jun 04, 2006 1:58 am
Posts: 136
gsegelk wrote:
I think what you are saying will solve my problem, but I don't know how to get access to the Connection object (for some reason, I feel really dumb saying that...it will probably hit me like a ton of bricks once I see what's going on!)

I currently go through the following steps before getting a Session object via factory.openSession():
1. Create a Configuration object
2. Use the 'setProperty' method on the Configuration object for db IP, port, user, password
3. Use the 'addResource' method on the Configuration object to set the mapping file
4. Create a SessionFactory using the 'buildSessionFactory' method on the Connection object
5. Create the session using the 'openSession' method on the SessionFactory object
6. Interact with the Session object (.save(), etc)

Is there a different way I should be initiating the connection manually? Thank you for your quick response and it seems like the issue is close to being solved!


after creating sessionFactory in step 5 do
Connection connection = mySessionFactory.openSession.connection();
// test connection

_________________
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 28, 2006 7:04 am 
Newbie

Joined: Wed Dec 27, 2006 12:36 pm
Posts: 3
Thanks for the help...I never would have thought about doing that. Is that a standard thing to do when setting up a connection?

As a side note, the 'getMetaData()' API says that it can throw a SQLException, but my catch method never catches it. If I catch just an Exception, then it works. It appears that the SQLException is being wrapped by org.hibernate.exception.GenericJDBCException? ...in the stack trace, the GenericJDBCException has the text 'Caused by: java.sql.SQLException: Access denied for user 'wronguser'@192.168.1.98 '. Not that it is important to answer this, but this was another thing that I found odd.


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.