This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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?
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().
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).]
Joined: May 05, 2000
If you are running a statement anyway, why not just use the statement as the test to see if the connection is alive?
Joined: Jul 06, 2000
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?
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