我正在尝试将组合模式的tomcat日志加载到MySQL数据库中,现在我将每个记录在分析时都推送到数据库。SQL连接性能
首先,读取从InputStream的管线,
然后将其解析到用于包装一个日志记录一个LogItem,
然后使用MySQLDbManager插入到DB中的记录,
MySQLDbManager持有数据库连接池的参考,一旦它被称为推日志,它会要求做SQL操作的连接,然后将其释放。
恐怕这是否是低性能与特点,因为MySQLDbMana ger会在日志推送期间获得并释放连接。
我以为做了一个List来保存所有的LogItems,在日志文件被读取后,我把这个列表传递给MySQLDbManager,但是这个日志文件可能拥有太多的记录,也就是说,它会导致一个LogItems的日志放入列表中,这是否安全?
这是两个方法:
MySQLDbManager dbm = MySQLDbManager.getInstance();
private void pushLogToDbByStream(BufferedReader br) {
String line;
int total = 0;
int error = 0;
try {
while ((line = br.readLine()) != null) {
LogItem li = LogParser.parser(line);
dbm.writeLogItemToDb(li);
total++;
}
} catch (IOException e) {
log.error("Error occur when read log from file");
} catch (IllegalStateException e) {
error++;
total++;
}
log.info(total + " items have been processed, " + error + " failed");
}
public int writeLogItemToDb(LogItem item) {
//get a connect from the connect pool
DBConnection dbc = pool.alloc();
String insertSql = "insert into log values" + item.buildSQLInsertValues();
//The buildSQLInsertValues() will return something like this--('127.0.0.1','GET','http://www.google.com'...)
int res;
try {
res = dbc.updateSQL(insertSql);
return res;
} catch (SQLException e) {
log.error("error occur when try to insert the item to db +\n" + insertSql + "\n");
return 0;
} finally {
//release the connection
pool.release(dbc);
}
}
还有更好的主意吗?
顺便说一句,如果家伙们认为我的问题太初级了,你可以拒绝回答,但请不要投我的帖子,我很难赢得声誉。 :)
是的,的确,我可以测试推送日志到db所花费的时间,但我不知道要测试多少个对象最多可以放在列表或映射中,只需将对象放到它们中,直到系统本身停止工作?无论如何,你的答案是“你不会为每个请求打开和关闭物理连接。”对我来说很有帮助。谢谢。 – hguser 2010-10-20 01:33:39