Also, don't forget to put your close() calls in a finally statement:
Connection conn = null;
Statement statement = null;
ResultSet resultSet = null;
try {
[do your thing]
}
finally {
if (resultSet != null ) try {resultSet.close();} catch (SQLException e)
{e.printStackTrace;}
if (statement != null ) try {statement.close();} catch (SQLException e)
{e.printStackTrace;}
if (conn != null) try {conn.close();} catch (SQLException e) {e.printStackTrace;}
}
The individual try-catch-statements around the the close() calls are often forgotten
but necessary.
-hendrik
> -----Ursprungliche Nachricht-----
> Von: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
> Auftrag von Kenneth Sizer
> Gesendet: Mittwoch, 23. Juni 2004 09:59
> An: Research Triangle Java User's Group mailing list.
> Betreff: RE: [Juglist] Should I explicitly close ResultSet and
> Statements?
>
>
> It is good practice, IMHO, to close what you open, but it's
> importance depends on the database. Most engines "do the right
> thing" when the connection is closed, but in some cases, I have
> seen crippling memory leaks on the database server when
> ResultSets are not explicitly closed.
>
> --Ken
>
> > -----Original Message-----
> > From: [EMAIL PROTECTED]
> > [mailto:[EMAIL PROTECTED] On Behalf Of Tony Spencer
> > Sent: Wednesday, June 23, 2004 9:45 AM
> > To: 'Research Triangle Java User's Group mailing list.'
> > Subject: [Juglist] Should I explicitly close ResultSet and Statements?
> >
> >
> > Is it necessary to close a ResultSet and PreparedStatement at
> > the end of some JDBC code before closing the Connection or
> > are they closed automatically by closing the database connection?
> >
> > For example is this ok?
> >
> > Connection conn = DbPool.getConnection();
> > ResultSet rs = null;
> > PreparedStatement = null;
> > .
> > .
> > .
> > conn.close();
> >
> >
> > Or should I do this?
> >
> > Connection conn = DbPool.getConnection();
> > ResultSet rs = null;
> > PreparedStatement = null;
> > .
> > .
> > .
> > rs.close();
> > stmnt.close();
> > conn.close();
> >
> >
> >
> > T O N Y S P E N C E R
> > Notsleepy LLC
> > 6512 Six Forks Rd.
> > Suite 502-B
> > Raleigh, NC 27615
> > Phone: 919.848.0691
> > Mobile: 415.637.6481
> > [EMAIL PROTECTED]
> >
> >
> >
> >
> > _______________________________________________
> > Juglist mailing list
> > [EMAIL PROTECTED]
> > http://trijug.org/mailman/listinfo/juglist_tri> jug.org
> >
>
> _______________________________________________
> Juglist mailing list
> [EMAIL PROTECTED]
> http://trijug.org/mailman/listinfo/juglist_trijug.org
>
_______________________________________________
Juglist mailing list
[EMAIL PROTECTED]
http://trijug.org/mailman/listinfo/juglist_trijug.org