我正在嘗試將組合模式的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