注意:在赛迪论坛上有一个笔者按照上述类似的方法没有运行成功,现将他所出现的问题和解决方法转载。
上述方法是利用传统的数据库连接方法,对于数据库的管理和效率严重不足,在现在这个连接池横行的时代,
为什么我们不能给给Log4j配上连接池,让Log4j利用数据连接池的连接和数据库进行通讯。
现查看Log4j的Api,发现JDBCAppender这个类有以下几段话:WARNING: This version of JDBCAppender is very
likely to be completely replaced in the future. Moreoever, it does not log exceptions. The JDBCAppender
provides for sending log events to a database.
For use as a base class:
Override getConnection() to pass any connection you want. Typically this is used to enable
application wide connection pooling.
Override closeConnection(Connection con) -- if you override getConnection make sure to
implement closeConnection to handle the connection you generated. Typically this would
return the connection to the pool it came from.
Override getLogStatement(LoggingEvent event) to produce specialized or dynamic statements.
The default uses the sql option value.
原来log4j建议我们把其提供的JDBCAppender作为基类来使用,然后Override三个父类的方法:getConnection(),
closeConnection(Connection con)和getLogStatement(LoggingEvent event)。
原来如此,那就写一个子类JDBCPoolAppender来替代这个JDBCAppender
文章来源于领测软件测试网 https://www.ltesting.net/