Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Connection Leaks

 
Anthony Andras
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi gang,

I recently came across some feedback on some code that I wrote that mentioned I had issues with connection leaks. From what I understand, a connection leak is when you don't close a connection after you have opened it. Is this correct?

An pseudo - code example of what I was doing that caused this feedback to occur is something like the following:

DbUtils - a class that handles the closing of a connection.
DatabaseInfo = a class that holds connection data.



The methods loadSomeData and loadSomeMoreData are private methods that simply open statements and result sets, then close them in a finally block. They then throw any exceptions that occur.

I understand this is a bit abstract, and I will write up a more detailed example if need be. Thanks guys.

Anthony
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In this pseudo-code Connection is declared as local inside try-catch block, and it is not visible in catch and finally blocks,
so this code cannot compile.
If this code compiles fine - it means that you close some other connection in finally block.
 
Charles Owen
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adding to the previous post, I would highly recommend you set your Connection objects to null. If you are using any sort of Connection Pooling, setting the Connection object to null returns the connection back to the Connection Pool so it can be reused. It also marks that specific Connection object as ready for garbage collection. Also, I would catch a SQLException and perhaps log the exception.

 
Jan Cumps
Bartender
Posts: 2602
13
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charles Owen wrote:... If you are using any sort of Connection Pooling, setting the Connection object to null returns the connection back to the Connection Pool so it can be reused. ...
It is returned to the pool when you call the close() method.

(although Sun says
When a Context instance is done with a connection (closed or garbage collected), the connection is returned to the pool for future use.
,
the Java community typically uses close() to return connections.
I would advise you to do the same. When you call close(), the connection is immediately returned to the pool. That is predictable behavior. Can you predict how/when/if the garbage collector handles it?)

Regards, Jan
 
Charles Owen
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right, I'm an idiot.

I learned my lesson. I am no longer going to post in these forums. I have a lot to learn. Thanks for correcting it.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charles Owen wrote: I am no longer going to post in these forums. I have a lot to learn.

It's ok to post even if you aren't sure of something. You can say "I think". That way if you are wrong someone will jump in. And you learn something either way!

I've posted things that I wasn't sure of with "I think" along with a few things that were flat out wrong. (such as when I was tired or misinterpreted the question) I was corrected. Not a big deal.
 
Jan Cumps
Bartender
Posts: 2602
13
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charles Owen wrote:You're right, I'm an idiot.

I learned my lesson. I am no longer going to post in these forums. I have a lot to learn. Thanks for correcting it.
Charles. I did not try to make you look like an idiot. This was my attempt to post a polite reply.
My excuses if I offended you.

Regards, Jan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic