今天在hangshi又发现了客户端集体突然灰屏的现象,检测发现应用服务器运行正常,数据库服务器正常,后来chenjun连线发现是apusic中的数据库连接已被耗尽所致,暂时重启了服务器,其实前天中午也发现了同样的问题,只是没有引起重视。
后来看了代码发现现场有些代码是调用存储过程的,有些是用statement执行sql的,都没有主动释放connection,虽然不见得一定会释放不掉,但某些情况下就可能不会释放,所以建议现场对于执行单条sql的用DBUtil类的方法,对于用statement来执行的就需要在最后用SQLUtils.cleanup()来释放,代码如下:
Connection connection;
Statement stmt;
ResultSet rs;
ArrayList result;
strSQL = getAdjustSQL(conditions, extConditions);
strSQL = strSQL + " order by Fyear,Fperiod,companynumber,itemnumber";
connection = getConnection(ctx);
if(connection == null)
throw new BOSException("Can't get Connection!");
stmt = null;
rs = null;
result = null;
try
{
stmt = connection.createStatement();
rs = stmt.executeQuery(strSQL);
result = getResultArray(ctx, rs, conditions);
}
catch(SQLException e)
{
throw new SQLDataException(e);
}finally{
SQLUtils.cleanup(rs, stmt, connection);
}