aspose file tools*
The moose likes JDBC and the fly likes finally block is not executing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "finally block is not executing" Watch "finally block is not executing" New topic
Author

finally block is not executing

J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 1066
    
  13

I'm posting this here since it involves a connection object and a "connection is closed error" which led me to this puzzling behavior. This is the code for a DAO which is called from a servlet.


I set breakpoints on this and stepped through it. Everything works correctly and the value of urlResult is the single record expected from the database, but as soon as the while loop ends, the code jumps to the return line and the finally never executes. When it reaches the return line, the values are as follows:

rset = (org.apache.commons.dbcp.DelegatingResultSet) org.apache.commons.dbcp.DelegatingResultSet@5706de
ps = (org.apache.commons.dbcp.DelegatingPreparedStatement) STMT0001
connection = (org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper) null
urlResult = (java.lang.String) breakingnews_6300.html

Up to this point, no exceptions have been thrown, but the value of connection is odd because it's null, but it's not really null as you'll see in a moment.

One more step and the above method returns to the servlet at the "url=urlDAO.getSingleURL(dept)" line below.

In this code the finally block does run, but it tries to close the connection because of the odd null-but-not-null value of the connection and of course throws a SQLException of "Connection is closed".

What am I doing wrong? Maybe I shouldn't be passing a reference to the connection from servlet to DAO but rather have the DAO create the connection? And why is the finally block in the DAO not running at all? I didn't think that was possible except under rare circumstances such as killing a thread or doing a system.exit in the try block.

Thanks for any suggestions.


"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." -- Ted Nelson
J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 1066
    
  13

More info...I removed the e.printStackTrace() lines from the finally block, because NetBeans was complaining about them, so the code looks like this and now the finally block runs. Why?


However, I still have the issue with the finally block in the servlet trying to close the null-but-not-null connection and throwing an error. Can anyone explain that one?
Is there a better way to close the connection instead of the "if(conn != null)" test?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

I agree with you that the "connection" variable can't be null. So instead the toString() method of that connection object returns the string "null".
Scott Selikoff
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3716
    
    5

First, some quick tips: I wrote an article about finally closing JDBC resources a few years back that might be helpful.

You can use Connection.isClosed() along with the null check. The more common code, though, is what you have where you check for null, attempt to close regardless, and exit. Trying to close an already closed connection and throwing a swallowed exception doesn't usually cause a problem in practice, whereas failing to close a connection definitely can cause issues. In other words, when it comes to closing connections it's better to be extra cautious than forgetful.


My Blog: Down Home Country Coding with Scott Selikoff
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: finally block is not executing