Knowing When To ResultSet.Close() and When to Close Your Mouth

Just a quick story from a recent conference we spoke at. At a lot of these conferences or User’s Group meetings there is a question and answer period in which attendees ask questions of all the speakers. The questions I have heard have generally been technical in nature and since most of the speakers are pretty technical, there is a lot of expertise on the “panel”.

At a recent conference, an attendee asked a straightforward Java question. He wanted to know why after sometime of running his Java application, which was a web application running WebLogic, his application would start getting errors from the database that there were too many cursors open.

This is a pretty common problem for beginning Java developers using JDBC and I remember one time about 10 years ago (there I go dating myself again), when I was first working with JDBC I had a DBA come running over to me complaining that I was using up 50 connections to her precious development database. This was my first week at the job and she was an intimidating person. It was then that I learned all about closing connections, statements, and result sets when using JDBC.

Anyway, somebody there on the panel started immediately with “I don’t mean to be rude, but you can’t expect us to answer a question like that without knowing more details about your situation, like what is the application trying to acheive, why is it using JDBC, why are you retrieving a REF_CURSOR…?” and on and on a bit.

I finally managed to get a word in and told the guy, “You should make sure that you are closing your ResultSets and even your Statements in the finally clause of a try/catch block.” This apparently didn’t satisfy the first guy who continued to go on for another 20 minutes about how the every search is now a pagination and it doesn’t make sense to get an unlimited set of results if you are only showing 10 on the screen and how you should really use a PL/SQL Collection to and a stored procedure to return the exact set of results you want, and blah, blah, blah.

I again managed to interupt enough to put in my two cents that if you are forgetting to close ResultSets in JDBC you probably don’t want to bother trying to return PL/SQL Collections yet.

I explained this to my wife, who isn’t technical by comparing it to someone asking a question about cooking, whenever I cook chicken in a frying pan (non-teflon!) it sticks to the pan, what am I doing wrong? to which the simple reply would be “add some oil to the pan first”. The guy I am describing would answer with an entire explanation of why you should bake your chicken instead and insist you can’t get a good answer unless he knew all the spices you were using and the dish you were trying to cook.

I thought it was kind of funny and it was really no surprise that the first guy’s talk was on what? You guessed it…”PL/SQL Collections.”

Leave a reply

You must be logged in to post a comment.