Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Does closing Connection always close related JDBC objects

 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As some of you know, closing a Connection also closes any JDBC objects (like Statement) created from that Connection. But I've just had a bout of uncertainty: suppose the Statement is referenced by a local variable which goes out of scope at the end of the method, but the Connection is closed after the method ends. Will the Statement still be closed? Here is some pseudo code to demonstrate what I'm saying.

 
stu derby
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A properly programmed JDBC driver will have references in the Connection object allowing it to close all child objects before it itself is closed.

Some people swear that not all drivers are properly programmed and insist on explicitly closing all resources themselves; it's not a bad practice anyway and can't hurt, it gets your code in a state where its easier to add connection pooling, for one thing...

But, yes, it's supposed to, even in your example scenario.
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, and I should have said that we are using a WebLogic Server connection pool which connects to an Oracle DB. I think we are using Oracle's thin driver.

I confused myself by thinking that the Statement object would be unreferenced at the end of the method. This would be true when the stmt method local variable no longer exists at the method end, but the Connection should still reference the Statement it creates.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic