• 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

DB connection..

 
Ranch Hand
Posts: 264
  • 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 interfaces with a database 99% of the methods that access the database are in one class(not sure I like the design but its code I "inherited"). Each method opens its own connection and then closes the connection. If it happens to throw an exception the application gets hung. Most of the exception handling/catching was/is happening in the action classes (most of the methods in the "monolithic db class" throw their exceptions).

I think this causes the application to hang when an exception is thrown and the connection isn't closed but I'm not certain.

Will adding something like

in the db methods help alleviate this problem?
Can I add that and still throw the exception?
I'm not terribly familiar with the finally block and its behavior (other than I heard that its supposed to be executed no matter what). What happens if something in the finally block throws an exception?

-Tad
 
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is actually a question regarding either JDBC or Exception handling, and not Struts. Exception handling seems to be the main thrust of it though, so I'm going to move this to Java in General (Intermediate).
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, this kind of cleanup is just what finally is for and us worth doing here. You might want to enclose each close in yet another try-catch so a failure in one won't bypass the others. Because nested try-catch is ugly and you will do this in mnay places you might like a closeAll() method that does not throw exceptions.

This may not help with your lock-up issues. Exceptions from your db class must be causing some nasty problems upstream in the call stack.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Notice that you only need to close the connection - associated statements and result sets will then be closed automatically, when needed.
 
reply
    Bookmark Topic Watch Topic
  • New Topic