File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Dose anyone know a way of checking if JDBC Connection is alive? I have found out that Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark "Dose anyone know a way of checking if JDBC Connection is alive? I have found out that" Watch "Dose anyone know a way of checking if JDBC Connection is alive? I have found out that" New topic
Author

Dose anyone know a way of checking if JDBC Connection is alive? I have found out that

Michal Harezlak
Ranch Hand

Joined: Jul 06, 2000
Posts: 185
Dose anyone know a way of checking if JDBC Connection is alive? I have found out that the connection will become null (garbage collected?) if RDBMS is inactive (down) for a while, but unfortunately that dose not apply if I (for example) will unplug the network. I was trying to use isClosed() of Connection, but that returns true unless you close it using close (makes sense), but if I try to execute a statement it will throw SQLException ´┐ŻDatabase not connected´┐Ż. I am thinking about using simple select query as a sort of database ping. It dose not seems to be a very elegant connection though.
Any ideas?
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
I have found that the only way to test for sure is to run a test query. For sybase we ran something like "select getdate()" so we didn't have to actually hit any tables. You could try running a getMetaData().


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Michal Harezlak
Ranch Hand

Joined: Jul 06, 2000
Posts: 185
Thanks Thomas,
I was consideration this alternative. I am a bit worried about the performance if every time before using a connection I will query the database. I cannot think about anything else, though. I will profile this solution to see how heavy it is.
Thanks again.

[This message has been edited by Michal Harezlak (edited January 23, 2001).]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
If you are running a statement anyway, why not just use the statement as the test to see if the connection is alive?
Michal Harezlak
Ranch Hand

Joined: Jul 06, 2000
Posts: 185
Originally posted by Thomas Paul:
If you are running a statement anyway, why not just use the statement as the test to see if the connection is alive?

That would involve significantly more work.
The App (Web Objects) used to open connection every time it was going to execute something (stored proc/fun query) on the RDB, after it was done it was closing it. This approach was not very efficient since the App makes quite a few trips to the RDB (Oracle 8). During reengineering of this app I have moved the connection to a singleton class that keeps just one connection (it is single-treaded app so no pooling necessary) and all the rest objects would request the connection from this guy. Is there any danger in this approach?
Brett Spell
Ranch Hand

Joined: Feb 24, 2001
Posts: 96
I once suggested to a member of the JDBC team that they add support for this type of "ping" functionality to the API, but was told that the idea had already been considered and rejected. Apparently, not many existing DBMS products can support the functionality, so they decided not to do this because the it "wouldn't be widely supported" in JDBC drivers. Personally, I don't find that to be a very compelling reason for leaving it out, but in any case, that's the semi-official reason that it isn't available.
------------------
Brett Spell
Author, Professional Java Programming
 
GeeCON Prague 2014
 
subject: Dose anyone know a way of checking if JDBC Connection is alive? I have found out that