Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Michal Harezlak
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
Michal Harezlak
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic