• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reconnecting to Database

 
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help. I appreciate it!!
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic