Re: Memory leak ?

From: Lew <noone(at)lewscanon(dot)com>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: Memory leak ?
Date: 2011-04-06 22:34:50
Message-ID: inipq8$o3h$1@news.albasani.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Mikko Tiihonen wrote:
> Pierre Le Mouëllic wrote:
>> With this code :
>>
>> public class TestJDBC {
>>
>> private static Timer timer;
>>
>> private static Connection connDB;
>> private static PreparedStatement sQLStatement;
>>
>> public static void main(String[] args) throws SQLException, ClassNotFoundException {
>>
>> connexion("jdbc:postgresql://praslin.qual.dc1:5432/G01DPM","postgres8","password");
>>
>> timer = new Timer();
>> timer.schedule(new local_task(), 1000L, 1000);
>> }
>>
>> static class local_task extends TimerTask {
>>
>> public void run() {
>> ResultSet rs=null;
>> try {
>> sQLStatement.setInt(1, 2602);
>> rs = sQLStatement.executeQuery();

> Missing
> rs.close();

Not needed.

From <http://download.oracle.com/javase/6/docs/api/java/sql/ResultSet.html>:

"A ResultSet object is automatically closed when the Statement object that
generated it is closed, re-executed, or used to retrieve the next result from
a sequence of multiple results."

>> sQLStatement.setInt(1, 2604);
>> rs = sQLStatement.executeQuery();

> Missing
> rs.close();

Nope.

In fact, it's rather an antipattern to call 'ResultSet#close()' explicitly.

>> sQLStatement.setInt(1, 2605);
>> rs = sQLStatement.executeQuery();
>> } catch (SQLException e) {
>> }
>> finally{
>>
>> try {
>> if(rs!=null)
>> rs.close();

What's bizarre here is that we aren't closing the statement.

That's weird.

>> } catch (SQLException e) {
>> }
>>
>> rollBack();
>> }
>> }
>> }

--
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg

In response to

Browse pgsql-jdbc by date

  From Date Subject
Next Message Lew 2011-04-06 22:40:36 Re: Memory leak ?
Previous Message John Lister 2011-04-06 21:51:46 Recommended resource type and settings for J2EE/Glassfish connection pool