答复: 用log4j把日志异步写入数据库中

[url]http://www.iteye.com/topic/172125[/url]

BufferSize没什么作用,JDBCAppender

只是把用户输出的log现在一个ArrayList中保存,当其数量达到了BufferSize,才启动写日志。而且JDBCAppender还是每次都生成Connection,每一个Log还是单独写入,不是批量写入。

Log4J的数据库写入方式就是一个鸡肋,如果需要提高性能,首先,自己需要实现一个连接池。而且其还不支持addBatch,赫赫,如下是log4j源代码(JDBCAppender.java)
########################
public void flushBuffer() {
//Do the actual logging
removes.ensureCapacity(buffer.size());
for (Iterator i = buffer.iterator(); i.hasNext();) {
try {
LoggingEvent logEvent = (LoggingEvent)i.next();
String sql = getLogStatement(logEvent);
execute(sql);
removes.add(logEvent);
}
catch (SQLException e) {
errorHandler.error("Failed to excute sql", e,
ErrorCode.FLUSH_FAILURE);
}
}

// remove from the buffer any events that were reported
buffer.removeAll(removes);

// clear the buffer of reported events
removes.clear();
}
###########################

protected void execute(String sql) throws SQLException {

Connection con = null;
Statement stmt = null;

try {
con = getConnection();

stmt = con.createStatement();
stmt.executeUpdate(sql);
} catch (SQLException e) {
if (stmt != null)
stmt.close();
throw e;
}
stmt.close();
closeConnection(con);

//System.out.println("Execute: " + sql);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值