I have an application that is constantly running 24/7. In the event of a network outage, this application has to be restarted, as when a user tries to use it, it will tell them the connection does not exist. I am not using connection pooling. Is there a way that I can find out if the connection still exists, and if it doesn't, reconnect to the database. I recently tried getting the connection object to see if it was null, but it always shows not null... even if I am not on the network. Here is my database connection class:
I take it you can't or don't want to use a connection pool (which generally close a connection after a certain amount of idle time, and re-open it, thus working around the issue of connections becoming stale).
In case you get the exception that the connection doesn't exist, you could close the connection, open a new one, and retry whatever it was that the code did. This could be automated so that whenever a query is sent, some dummy command SQL query is executed, and if there's an exception, the connection is closed and re-opened before the actual command is executed. That would incur a slight performance penalty, which may or may not be acceptable in your case.
Ping & DNS - updated with new look and Ping home screen widget
Joined: Feb 28, 2001
To tell you the truth, I really don't know a whole heck of a lot about connection pooling. It is something that I am planning on looking into in the future. However, for the time being, I believe I may have found a solution. I created a method in my connection class that checks to see if the connection is closed :
This method is ran whenever the window gains focus. I have tested this out and it seems to work... however I still can't help but wonder... is this a very efficient way to handle my problem? What are the downfalls of doing this? [ August 13, 2007: Message edited by: Jennifer Sohl ]
Joined: Mar 22, 2005
Using a pool isn't necessary; I mentioned it because it does keep connections alive automatically, without the application code needing to do anything.
If it happens regularly that connections go stale, then you probably want to perform the check every time you make a DB call.
Note that the Class.forName call is necessary only once per JVM lifetime, not every time you get a connection.
DB2 has some slick options here. Ask the folks who install and care for the software about Automatic Client Reroute. The docs talk mostly about failing from a primary server to a backup server, but it can also reconnect to a single server. It's tunable for how often and how many times it retries.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi