Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Resource Management in JDBC

 
Brian Mulholland
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So my philosophy in writing JDBC code has always been to diligently close resources like connections and resultsets and statements in finally clauses. But I am coming on to a project where the developers don't. They close connections (most of the time anyway...I found a few spots where they weren't upon some conditions). But they are in the habit of NOT closing resultsets and preparedstatements. My vague recollection is that not closing these can (depending on how well the driver or pool cleans up after you which is implementation dependent) create memory leaks, leave open cursors on the DB side, and even create connection leaks. But what I'd like to ask is what EXACTLY are the consequences of these things, so I can scare these people straight?

1) What are the consequences of not closing resultsets if you close everything else?
2) What are the consequences of not close preparedstatements if you close everything else?
3) Can either 1 or 2 create connection leaks even if conn.close() is being invoked?

And if there are driver-specific consequences, can anyone tell me what they are with Oracle?
 
Gian Franco
blacksmith
Ranch Hand
Posts: 979
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Brian,

...quickly a few things...and probably not detailed enough to
answer your question, but anyway...

1.) access violation errors
2.) corresponding resources are not freed

I recall having a difficult to debug performance problem
because of this...

I'd like to see the experience of other people as well...
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35266
383
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that it depends on the driver. They may be fine for the time being and then have the app fall down if they switch/upgradeable to JDBC driver to one that doesn't handle implicit closes well.

It is worse when using a connection pool. Then close() returns the resource to the pool rather than closing the connection. Which makes it more likely the statement/result set isn't closed automatically.

I don't know what the consequences are with Oracle because we always close resources. Or more specifically, we use a framework that closes them for us. There's enough things that can go wrong without a coding anti-practice that invites more!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic