• Post Reply Bookmark Topic Watch Topic
  • New Topic

DB connection..  RSS feed

 
Tad Dicks
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
  • 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
 
Jason Menard
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
  • 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).
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!