File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Connection Leaks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Connection Leaks" Watch "Connection Leaks" New topic
Author

Connection Leaks

Anthony Andras
Ranch Hand

Joined: Nov 09, 2008
Posts: 31
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

Joined: Jun 21, 2008
Posts: 423
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

Joined: Aug 31, 2009
Posts: 61
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

Joined: Dec 20, 2006
Posts: 2477
    
    7

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

OCUP UML fundamental and ITIL foundation
youtube channel
Charles Owen
Ranch Hand

Joined: Aug 31, 2009
Posts: 61
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 29247
    
139

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2477
    
    7

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Connection Leaks
 
Similar Threads
Good Programming Practice
SQL Server - Connection pool leak
Please help clarify this!
When to use Instance vs Local variables
Proper way to close Connection, Statement & ResultSet