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?
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).
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.
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
Notice that you only need to close the connection - associated statements and result sets will then be closed automatically, when needed.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop