关于在Log4j中使用JDBCAppender时出现死循环的问题
APACHE的log4j是一个非常好用的日志记录管理工具,可以实现到屏幕、文件、远程数据库、自动发送邮件等,功能强大而又简单易用。
但是今天在使用经过扩展的JDBCAppender时却碰到一个莫名其妙的问题,描述如下:
1.为了在日志向数据输出时每次都创建新的连接,在原来JDBCAppender的基础上进行扩展,使用自己写的数据库连接池,主要是重写getConnectioin()和closeConnection()两个方法,未改动前的源代码如下:
import java.sql.Connection;
import
java.sql.SQLException;
import java.util.ArrayList;
import org.apache.log4j.jdbc.JDBCAppender;
import org.apache.log4j.spi.LoggingEvent;
import com.gftech.util.GFConn;
import com.gftech.util.GFDB;
public class JDBCExtAppender extends JDBCAppender {
protected String driver;
public static GFDB gfdb;
private ArrayList <GFConn> tempList;
public JDBCExtAppender() {
super();
tempList = new ArrayList <GFConn> ();
}
/**
* Override this to return the connection to a pool, or to clean up the
* resource.
*
* The default behavior holds a single connection open until the appender is
* closed (typically when garbage collected).
*/
protected void closeConnection(Connection con) {
if (con != null && tempList != null) {
for (int i = 0; i < tempList.size(); i++) {
GFConn gfconn = tempList.get(i);
if (con == gfconn.getConn()) {
gfconn.close();
tempList.remove(i);
//
System.err.println( "remove conn: "+con);
break;
}
}
}
}
/**
* Override this to link with your connection pooling system.
*
* By default this creates a single connection which is held open until the
* object is garbage collected.
*/
protected Connection getConnection() throws
SQLException {
if (gfdb == null) {
gfdb = new GFDB( "db9 ", driver, databaseURL, databaseUser, databasePassword);
}
if (gfdb != null) {