Meaningless Drivel is fun!
The moose likes JDBC and Relational Databases and the fly likes Detect a lost of connection. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Detect a lost of connection." Watch "Detect a lost of connection." New topic

Detect a lost of connection.

Nicolas Lavacry

Joined: Jul 05, 2004
Posts: 1
How could I detect correctly a lost of connection without exception catching ?

Is there any listener or method to implement ?

Best Regards.
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

No. Its exceptions or nothing.

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Andy Nimmo

Joined: Mar 30, 2003
Posts: 14

That's not strictly true. Although exceptions are the best way of dealing with an occurance of a showstopping situation there are other ways to do things.

In this example Nicolas wants to just check if the connection is still alive and well which can be done quickly and easily without waiting for an exception to be thrown.

Have a look at the following code which I am using in one of my database access classes:

This simple example checks to see if the connection is alive before it attempts to send a Query. If the connection isn't alive then it attempts a single re-establishment of the connection before failing.

Hope this helps.

Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

Doh! Well caught Andy. That will teach me to answer questions without double checking the docs.
ramandeep mann

Joined: Aug 17, 2006
Posts: 13
But What if query is already been fired and server is waiting for reply.On the other side Firewall or something else drop the connection before receiving the reply.I am in such a situation

We have such a query which takes more then 2 hours and database is on other machine.But my application server is behind the firewall so this firewall is dropping the ideal connection (waiting for results )after every hour .

I agree. Here's the link:
subject: Detect a lost of connection.
jQuery in Action, 3rd edition