You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by David Sitsky <si...@nuix.com.au> on 2001/10/24 09:09:14 UTC

JDBCMailRepository connection leak?

G'day,

I was browsing though JDBCMailRepository.java, and noticed in many of the 
methods (store, retrieve, remove, list), a JDBC connection is obtained, 
but in the event of an exception being thrown, the connection isn't closed 
in the catch() block, which will result in a connection leak, since 
connection.close() isn't called, although the connection object be garbage 
collected of course.

The code should be refactored to have a finally() block so that the code 
looks like:

method()
{
    try
    {
        Connection c = getConnection();

        ...
    }
    catch (Exception e)
    {
        ...
    }
    finally
    {
        c.close();
    }
}

-- 
Cheers,
David

---------------------------------------------------------------------
To unsubscribe, e-mail: james-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: james-dev-help@jakarta.apache.org


Re: JDBCMailRepository connection leak?

Posted by David Sitsky <si...@users.sourceforge.net>.
No worries - thanks for putting it in. :)

On Thursday 01 November 2001 16:03, you wrote:
> Thanks David.  I went through JDBCMailRepository and added finally
> blocks to close no matter what.
>
> Serge Knystautas
> Loki Technologies - Unstoppable Websites
> http://www.lokitech.com/
> ----- Original Message -----
> From: "David Sitsky" <si...@nuix.com.au>
> To: <ja...@jakarta.apache.org>
> Sent: Wednesday, October 24, 2001 2:59 AM
> Subject: Re: JDBCMailRepository connection leak?
>
> > Actually... its not a connection leak (my apologies).  I was used to
> > an environment where there was a connection pool, so the connection
> > itself was never GCed since there was a reference to it... :)
> >
> > In this case, there won't be a reference to c, so the resources will
> > get cleaned up when the object is GCed (whenever that will be).  Still
> > probably better style to use finally() though..
> >
> > Cheers,
> > David
> >
> > On Wednesday 24 October 2001 17:09, David Sitsky wrote:
> > > G'day,
> > >
> > > I was browsing though JDBCMailRepository.java, and noticed in many
> > > of the methods (store, retrieve, remove, list), a JDBC connection is
> > > obtained, but in the event of an exception being thrown, the
> > > connection isn't closed in the catch() block, which will result in a
> > > connection leak, since connection.close() isn't called, although the
> > > connection object be garbage collected of course.
> > >
> > > The code should be refactored to have a finally() block so that the
> > > code looks like:
> > >
> > > method()
> > > {
> > >     try
> > >     {
> > >         Connection c = getConnection();
> > >
> > >         ...
> > >     }
> > >     catch (Exception e)
> > >     {
> > >         ...
> > >     }
> > >     finally
> > >     {
> > >         c.close();
> > >     }
> > > }
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: james-dev-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: james-dev-help@jakarta.apache.org
>
> --
> To unsubscribe, e-mail:  
> <ma...@jakarta.apache.org> For additional
> commands, e-mail: <ma...@jakarta.apache.org>

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: JDBCMailRepository connection leak?

Posted by Serge Knystautas <se...@lokitech.com>.
Thanks David.  I went through JDBCMailRepository and added finally blocks to
close no matter what.

Serge Knystautas
Loki Technologies - Unstoppable Websites
http://www.lokitech.com/
----- Original Message -----
From: "David Sitsky" <si...@nuix.com.au>
To: <ja...@jakarta.apache.org>
Sent: Wednesday, October 24, 2001 2:59 AM
Subject: Re: JDBCMailRepository connection leak?


> Actually... its not a connection leak (my apologies).  I was used to an
> environment where there was a connection pool, so the connection itself
> was never GCed since there was a reference to it... :)
>
> In this case, there won't be a reference to c, so the resources will get
> cleaned up when the object is GCed (whenever that will be).  Still
> probably better style to use finally() though..
>
> Cheers,
> David
>
> On Wednesday 24 October 2001 17:09, David Sitsky wrote:
> > G'day,
> >
> > I was browsing though JDBCMailRepository.java, and noticed in many of
> > the methods (store, retrieve, remove, list), a JDBC connection is
> > obtained, but in the event of an exception being thrown, the connection
> > isn't closed in the catch() block, which will result in a connection
> > leak, since connection.close() isn't called, although the connection
> > object be garbage collected of course.
> >
> > The code should be refactored to have a finally() block so that the code
> > looks like:
> >
> > method()
> > {
> >     try
> >     {
> >         Connection c = getConnection();
> >
> >         ...
> >     }
> >     catch (Exception e)
> >     {
> >         ...
> >     }
> >     finally
> >     {
> >         c.close();
> >     }
> > }
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: james-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: james-dev-help@jakarta.apache.org
>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: JDBCMailRepository connection leak?

Posted by David Sitsky <si...@nuix.com.au>.
Actually... its not a connection leak (my apologies).  I was used to an 
environment where there was a connection pool, so the connection itself 
was never GCed since there was a reference to it... :)

In this case, there won't be a reference to c, so the resources will get 
cleaned up when the object is GCed (whenever that will be).  Still 
probably better style to use finally() though..

Cheers,
David

On Wednesday 24 October 2001 17:09, David Sitsky wrote:
> G'day,
>
> I was browsing though JDBCMailRepository.java, and noticed in many of
> the methods (store, retrieve, remove, list), a JDBC connection is
> obtained, but in the event of an exception being thrown, the connection
> isn't closed in the catch() block, which will result in a connection
> leak, since connection.close() isn't called, although the connection
> object be garbage collected of course.
>
> The code should be refactored to have a finally() block so that the code
> looks like:
>
> method()
> {
>     try
>     {
>         Connection c = getConnection();
>
>         ...
>     }
>     catch (Exception e)
>     {
>         ...
>     }
>     finally
>     {
>         c.close();
>     }
> }

---------------------------------------------------------------------
To unsubscribe, e-mail: james-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: james-dev-help@jakarta.apache.org