Meaningless Drivel is fun!*
The moose likes JDBC and the fly likes Reconnecting to Database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Reconnecting to Database" Watch "Reconnecting to Database" New topic
Author

Reconnecting to Database

Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
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:



Any help would be greatly appreciated!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
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
Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
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 ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
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.
Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
Thanks for your help. I appreciate it!!
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reconnecting to Database
 
Similar Threads
JDBC and JApplet
Help on objects
Statement is closed?
Could not locate DB driver
could not locate driver