-->
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.  [ 8 posts ] 
Author Message
 Post subject: How to test connection validity ?
PostPosted: Thu Sep 07, 2006 6:09 am 
Newbie

Joined: Thu Feb 02, 2006 9:39 am
Posts: 19
Hi,

In my application, users could dynamically change DB connection parameters (like database host or database username).

Since openSession() never fails, I wanted to test the session validity after opening it. "session.isConnected()" and "session.isOpen()" seems to always return true even if I'm badly mis-configuring my database (to an inexistant host for instance).

So until now I used a dummy HQL quey ("select 1"), and see if this execution throw an Exception, or not. This works perfectly on MySQL and PostgreSQL but not on Oracle (I could not blame Oracle on that one because "select 1" is not ANSI SQL).

OK, how do you guys test your connection validity ? Any tips ?

--
Hugo


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 11, 2006 4:11 am 
Newbie

Joined: Thu Feb 02, 2006 9:39 am
Posts: 19
Come on, don't be shy ;)

I'm sure I'm not the first guy in the universe that need to test DB connection through hibernate...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 11, 2006 7:04 am 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
try with:

Code:
boolean isConnected = session.isConnected() && ! session.connection().isClosed();


Please, don't forget to rate.


Top
 Profile  
 
 Post subject: use select 1 from dual
PostPosted: Mon Sep 11, 2006 7:12 am 
Newbie

Joined: Sun Nov 21, 2004 5:21 am
Posts: 14
in Hibernate2, hibernate.properties have a property "hibernate.dbcp.validationQuery" which is used exactly for that. the default value is "select 1 from dual".

it is not used in Hibernate3 however.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 11, 2006 8:31 am 
Newbie

Joined: Thu Feb 02, 2006 9:39 am
Posts: 19
Quote:
"select 1 from dual"

does not work on every DBMS. I already look for an universal SQL statement on newsgroup comp.databases, but such a query doesn't seems to exist...

and for the
Quote:
boolean isConnected = session.isConnected() && ! session.connection().isClosed();


it always returns true to me, even if I tried to connected to 0.0.0.0


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 11, 2006 11:57 am 
Senior
Senior

Joined: Wed Aug 17, 2005 12:56 pm
Posts: 136
Location: Erie, PA (USA)
I assume you are using a different dialect for each db. So, can you extend each dialect class and create a "testing" sql statement that can be used for that DB?

_________________
---- Don't forget to rate! ----


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 11, 2006 12:34 pm 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
I really doubt you have tried my suggestion.

Are you sure you copy it correctly?

Also you could try with session.getConnection().getMetaData()


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 11, 2006 12:57 pm 
Newbie

Joined: Thu Feb 02, 2006 9:39 am
Posts: 19
Quote:

I really doubt you have tried my suggestion.

You're perfectly right !

As I said, I tried "session.isConnected()" and "session.isOpen()" (which did not work as expected), but I mis-read your answer and did not see you use the underlying connection to make the "isClosed()" call. Sorry for the mistake :/

So thanks a lot !! It works perfectly now !

--
Hugo


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