• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Connection Leaks

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Bartender
Posts: 2661
19
Netbeans IDE C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2661
19
Netbeans IDE C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
I'm THIS CLOSE to ruling the world! Right after reading this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic