-->
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.  [ 2 posts ] 
Author Message
 Post subject: How to check if connection is valid/server exists?
PostPosted: Wed Aug 18, 2004 5:00 am 
Newbie

Joined: Tue Jul 27, 2004 1:33 pm
Posts: 18
Location: South Africa
Hibernate version: 2.1.6

Mapping documents: NA

Code between sessionFactory.openSession() and session.close():
Session session = (Session)Config.getInstance().getSession(server); //this does the openSession
StringBuffer str = new StringBuffer("from Sysdtspackagelog as p where p.versionid='");
str.append(this.versionId);
str.append("' and id='");
str.append(id);
str.append("'");
logger.debug("Query string = " + str.toString());
list = session.find(str.toString());
session.close();

Full stack trace of any exception that occurs: NA

Name and version of the database you are using: MSSQL 2000

Debug level Hibernate log excerpt: NA

Hi all,

I am writing an app that will dynamically create hibernate configurations to various MS SQL databases. Sometimes the database server may be offline or the server address is wrong so that the url for the database is ok but the server cannot be found.

I want to catch this exception. i.e. a non-existant server before any queries are issued against it. I thought that the method sessionFacotry.opeSession() would fail if it cannot find the server. However it seems to create a session ok. I was hoping to check for a null session or exception to stop processing right there. But as the session appears valid I then issue a query against it. Hibernate goes through the whole process of creating the query and then only fails when it issues the sql.

Is there a way to test the connection is valid before all this is done?

Thanks
Mark


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 18, 2004 5:24 am 
Senior
Senior

Joined: Fri Nov 21, 2003 5:55 am
Posts: 155
Hi, you can keep the connection with the session object and apply method on it.

Session session = sf.openSession();
Connection conn = session.connection();

If i understand well your question :)
But whn you try to openSession, you ll have an exception.

You can have more info with DatabaseMetaData object on te Connection


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