tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Pid <...@pidster.com>
Subject Re: DBCP abandoned trace - unable to understand the leak
Date Wed, 10 Nov 2010 08:51:01 GMT
On 04/11/2010 15:41, Mikolaj Rydzewski wrote:
> 
> On Thu, 4 Nov 2010 10:37:25 -0500, "Propes, Barry L "
> <barry.l.propes@citi.com> wrote:
>> Not sure if it matters or not, but in your   SponserSummaryDAO
>> method, it appears you establish the rs as null, but don't ever close
>> it? You might specifically try that.
>>
>> And is it necessary to reassign all those variables (connection, rs,
>> pstmt) to null again in those catch blocks?
> 
> One more reason to use well designed utilities like commons-dbutils or
> jdbc-template.
> 

Or at least to use a static method to close the db objects & stop
putting the same boiler plate in all of the finally blocks.


public class DB {

  public static void close(ResultSet obj) {
	if (obj == null)
	  return;
	try {
	  obj.close();
	}
	catch (SQLException e) {
	  // catch or log
	  e.printStackTrace();		
	}
  }

  public static void close(Statement obj) {
	if (obj == null)
	  return;
	try {
	  obj.close();
	}
	catch (SQLException e) {
	  // catch or log
	  e.printStackTrace();		
	}
  }

  public static void close(Connection obj) {
	if (obj == null)
	  return;
	try {
	  obj.close();
	}
	catch (SQLException e) {
	  // catch or log
	  e.printStackTrace();		
	}
  }

}

ResultSet rs = null;
PreparedStatement ps = null;
Connection cn = null;

try {
	// ... do DB stuff
}
catch (SQLException e) {
	// ... log or something
}
finally {
  DB.close(rs);
  DB.close(ps);
  DB.close(cn);
}



p

Mime
View raw message