在我的应用程序中,我需要输入cache
数据,HashMap
否则TreeMap
它会节省大量时间,因为每次从中获取记录DB server
并进行处理是非常耗时的任务。
我也在使用这个应用程序进行分析,JProfiler
我觉得当我从中获取记录DataBase
以放入Map
连接时ResultSet
并Statement
没有关闭,因为它向我展示了这些classes
占用了太多内存。
这是我对此反应过度还是真的是一个问题?
顺便说一句,我正在关闭connection
in finally block
。我正在发布它的代码。
public Map someFunction() throws SomeException{
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
String sql=null;
Map <String,String> testMap=new TreeMap<String, String>();
try {
con=HMDBUtil.getConnection();
if(cacheSize==0) {
sql = "SELECT SOMETHING FROM SOMEWHERE";
}else {
sql = "SELECT SOMETHING FROM SOMEWHERE_ELSE where rownum<"+cacheSize;
}
stmt=con.createStatement();
stmt.setFetchSize(100000);
rs=stmt.executeQuery(sql);
long count=0;
while(rs.next()) {
testMap.put(rs.getString(1).trim(), rs.getString(2));
count++;
}
} catch (SQLException e) {
log.fatal("SQLException while fetching Data",e);
throw new SomeException("SQLException while fetching Data",e);
}finally {
HMDBUtil.close(con, stmt, rs);
}
return testMap;
}
HMDBUtil.close() 方法---
public static void close(Connection con, Statement stmt, ResultSet rs)
throws SomeException {
if (log.isDebugEnabled())
log.debug("Invoked");
close(rs);
close(stmt);
close(con);
if (log.isDebugEnabled())
log.debug("Leaving");
}
所有用于关闭所有连接的关闭方法——
public static void close(Connection con) throws SomeException {
try {
if (log.isDebugEnabled())
log.debug("Invoked");
if (con != null) {
con.close();
con = null;
}
if (log.isDebugEnabled())
log.debug("Leaving");
} catch (SQLException e) {
log.fatal("SQLException while Closing connection ", e);
throw new SomeException("SQLException while Closing connection ",
e, false, true);
}
}
public static void close(Statement stmt) throws SomeException {
try {
if (log.isDebugEnabled())
log.debug("Invoked");
if (stmt != null) {
stmt.close();
stmt = null;
}
if (log.isDebugEnabled())
log.debug("Leaving");
} catch (SQLException e) {
// log.error("Exception while Closing statement ", e);
log.fatal("SQLException while Closing statement ", e);
throw new SomeException("SQLException while Closing statement ", e, false, true);
}
}
public static void close(ResultSet rs) throws SomeException {
try {
if (log.isDebugEnabled())
log.debug("Invoked");
if (rs != null) {
rs.close();
rs = null;
}
if (log.isDebugEnabled())
log.debug("Leaving");
} catch (SQLException e) {
log.fatal("SQLException while Closing rs", e);
throw new SomeException("SQLException while Closing rs", e, false, true);
}
}