log4j利用数据库连接池(dbcp)

时间:2022-07-10 21:50:37

最近写程序有个问题郁闷了好久,开始写的时候很多操作频繁的对数据库进行增删改查操作,有些操作中就出现了效率低下的问题。研究之后将所有对数据库的连接方式改用了数据库连接池(dbcp)的方式,果然各个操作的效率提高了很多,不过出现了一个问题,我的日志部分是利用log4j来实现的,在我对数据库操作中与一个功能是对数据库的备份和恢复,由于恢复数据库的功能的要kill掉进程再恢复,我发现dbcp是有自动重连机制的,所以恢复数据库后其他操作都正常,但是日志不再写入数据库,纠结了很久发现是log4j的配置,之前log4j是自己连数据库,在kill之后他没有重连,所以无法写入数据库,那么我就改了配置,让log4j也利用数据库连接池,问题就解决了。

之前的配置:

log4j.rootLogger = debug,stdout,D,E,db

### \u8F93\u51FA\u4FE1\u606F\u5230\u63A7\u5236\u62AC ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n

### \u8F93\u51FADEBUG \u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7\u5230=E://logs/error.log ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.DatePattern='.'yyyy-MM-dd
log4j.appender.D.File = logs/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n

### \u8F93\u51FAERROR \u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7\u5230=E://logs/error.log ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.DatePattern='.'yyyy-MM-dd
log4j.appender.E.File =logs/error.log
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
###JDBCAppender
log4j.appender.db = org.apache.log4j.jdbc.JDBCAppender
log4j.appender.db.BufferSize=1
log4j.appender.db.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
log4j.appender.db.user=sa
log4j.appender.db.password=123456
log4j.appender.db.URL=jdbc:sqlserver://localhost:1433;DatabaseName=RMS_Station

log4j.appender.db.sql=insert into T_ResLog (LogName,UserName,Class,Mothod,createTime,LogLevel,MSG) values ('%X{userId}','%X{userName}','%C','%M','%d{yyyy-MM-dd HH:mm:ss}','%p','%m')
log4j.appender.db.layout=org.apache.log4j.PatternLayout


改之后:

## \u8BBE\u7F6E###
log4j.rootLogger = debug,stdout,D,E,db

### \u8F93\u51FA\u4FE1\u606F\u5230\u63A7\u5236\u62AC ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n

### \u8F93\u51FADEBUG \u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7\u5230=E://logs/error.log ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.DatePattern='.'yyyy-MM-dd
log4j.appender.D.File = logs/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n

### \u8F93\u51FAERROR \u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7\u5230=E://logs/error.log ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.DatePattern='.'yyyy-MM-dd
log4j.appender.E.File =logs/error.log
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
###JDBCAppender
# log4j.appender.db = org.apache.log4j.jdbc.JDBCAppender
# log4j.appender.db.BufferSize=1
# log4j.appender.db.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
# log4j.appender.db.user=sa
# log4j.appender.db.password=123456
# log4j.appender.db.URL=jdbc:sqlserver://localhost:1433;DatabaseName=RMS_Station

# log4j.appender.db.sql=insert into T_ResLog (LogName,UserName,Class,Mothod,createTime,LogLevel,MSG) values ('%X{userId}','%X{userName}','%C','%M','%d{yyyy-MM-dd HH:mm:ss}','%p','%m')
# log4j.appender.db.layout=org.apache.log4j.PatternLayout
log4j.appender.db.sql=insert into T_ResLog (LogName,UserName,Class,Mothod,createTime,LogLevel,MSG) values ('%X{userId}','%X{userName}','%C','%M','%d{yyyy-MM-dd HH:mm:ss}','%p','%m')
log4j.appender.db=com.sm.tools.JDBCPoolAppender
log4j.appender.db.layout=org.apache.log4j.PatternLayout


重写

/**
* 重写log4j的数据库连接池(DBCP)连接
*/

package com.sm.tools;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.log4j.spi.ErrorCode;

import com.sm.db.DBManager;

public class JDBCPoolAppender extends org.apache.log4j.jdbc.JDBCAppender {
/**数据库连接对象*/


protected Connection connection;


public JDBCPoolAppender() {
super();
}


@Override
protected void closeConnection(Connection con) {
try {
if (connection != null && !connection.isClosed())
connection.close();
} catch (SQLException e) {
errorHandler.error("Error closing connection", e, ErrorCode.GENERIC_FAILURE);
}
}


@Override
protected Connection getConnection() throws SQLException {
try {
//connection=JdbcUtils.getConnection();
connection=DBManager.getConn();
System.out.println("log4j连接标记");
} catch (Exception e) {
System.out.println(e.getMessage());
}
return connection;
}
}