java PreparedStatement需要关闭,不然会内存溢出

来源:互联网 发布:js代码在线运行 编辑:程序博客网 时间:2024/06/04 18:09

“第三方的数据库连接池,使用的时候,获取到Connection之后,使用完成,调用的关闭方法(close()) ,并没有将Connection关闭,只是放回到连接池中,如果调用的这个方法,而没有手动关闭PreparedStatement等,则这个PreparedStatement并没有关闭,这样会使得开发的程序内存急速增长,java的内存回收机制可能跟不上速度,最终造成Out of memory Error”
参考:http://blog.csdn.net/ghostgant/article/details/16860927、




错误的代码示例:


         con = dao.openBaseConnection() ;
            UUID id ;
            while( count < 10*10000){
                id = UUID.randomUUID() ;
                recordId = (id.getLeastSignificantBits()&0xfffffff)+"" ;
                PreparedStatement preStat = con.prepareStatement(sql) ;
                preStat.executeUpdate() ;
                 //forget to close the preStat
            }

com.mysql.jdbc.ConnectionImpl类
......
 private Map<Object, Object> cachedPreparedStatementParams;
.......

 PreparedStatement.ParseInfo pStmtInfo = (PreparedStatement.ParseInfo)this.cachedPreparedStatementParams.get(nativeSql);

.....


内存分析工具mat工具参考:http://blog.csdn.net/yxz329130952/article/details/50288145


0 0
原创粉丝点击