老应用转druid并启用慢sql记录功能

1/在src文件夹下建立db.druid.properties

driverClassName=org.postgresql.Driver
url=jdbc:postgresql://yourserver:5432/yourdb
username=youraccount
password=yourpasswd
filters=stat,wall
initialSize=5
maxActive=20
maxWait=60000
timeBetweenEvictionRunsMillis=60000
minEvictableIdleTimeMillis=300000
validationQuery=SELECT 1
testWhileIdle=true
testOnBorrow=false
testOnReturn=false
poolPreparedStatements=false
maxPoolPreparedStatementPerConnectionSize=200

2/建立ConnectionProvider类,因为不是spring app所以一切手写...

package com.freestyle.db;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.log4j.Logger;

import com.alibaba.druid.filter.logging.Slf4jLogFilter;
import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

//@Service
public class ConnectionProvider {
	private final static Object _lockObj="ConnectionProvider";
	private static DruidDataSource druidDataSource=null;
	public static Connection getConnection() throws SQLException {
		synchronized(_lockObj) {
			if (druidDataSource==null) {
				initWithConfig("db.druid.properties");
			}
		}
		return druidDataSource.getConnection();
	}
	public ConnectionProvider() {
		getDataSource();
		//org.apache.ibatis.logging.LogFactory.useLog4JLogging();
	}
	private static Properties loadPropertiesFile(String fullFile) {
		String webRootPath = null;
		if (null == fullFile || fullFile.equals("")) {
			throw new IllegalArgumentException("Properties file path can not be null" + fullFile);
		}
		webRootPath = ConnectionProvider.class.getClassLoader().getResource("").getPath();
		//webRootPath = new File(webRootPath).getParent();
		InputStream inputStream = null;
		Properties p = null;
		try {
			inputStream = new FileInputStream(new File(webRootPath + File.separator + fullFile));
			p = new Properties();
			p.load(inputStream);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (null != inputStream) {
					inputStream.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return p;
	}
	//@Bean(name="dataSource")
	public DataSource getDataSource() {
		synchronized(_lockObj) {
			if (druidDataSource==null) {
				Logger.getLogger("ConnectionProvider").info("Init data source.");
				initWithConfig("db.druid.properties");
			}			
		}
		return druidDataSource;
	}
	public static boolean initWithConfig(String pvConfigFile) {
		Properties properties = loadPropertiesFile(pvConfigFile);
		try {
			druidDataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties); // DruidDataSrouce工厂模式
			StatFilter lvStatFilter=new StatFilter();
			lvStatFilter.setSlowSqlMillis(10);
			lvStatFilter.setLogSlowSql(true);
			
			Slf4jLogFilter lvLogFilter=new Slf4jLogFilter();
			lvLogFilter.setDataSourceLogEnabled(true);
			lvLogFilter.setStatementExecutableSqlLogEnable(true);
			druidDataSource.getProxyFilters().add(lvStatFilter);
			druidDataSource.getProxyFilters().add(lvLogFilter);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
			//log.error("获取配置失败");
		}
	}	
}

3/修改web.xml,在后面加上:

  
    DruidWebStatFilter
    com.alibaba.druid.support.http.WebStatFilter


    exclusions
    *.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*




    DruidWebStatFilter
    /*



    DruidStatView
    com.alibaba.druid.support.http.StatViewServlet
      
         
        loginUsername  
        druid  
      
      
         
        loginPassword  
        ***  
       



    DruidStatView
    /druid/*

 

4/修改log4j.properties, 

log4j.rootLogger=DEBUG,stdout,RollingAppender,druid

# Druid    -------------------增加Druid监控日志输出
log4j.logger.druid.sql=WARN,druid
log4j.logger.druid.sql.DataSource=WARN,druid
log4j.logger.druid.sql.Connection=WARN,druid
log4j.logger.druid.sql.Statement=WARN,druid
 
log4j.appender.druid=org.apache.log4j.DailyRollingFileAppender
log4j.appender.druid.layout=org.apache.log4j.PatternLayout
log4j.appender.druid.layout.ConversionPattern= [%d{HH\:mm\:ss}] %c{1} - %m%n
log4j.appender.druid.datePattern='.'yyyy-MM-dd
log4j.appender.druid.Threshold = WARN
log4j.appender.druid.append=true
#log4j.appender.druid.File=${catalina.home}/logs/ssm-maven/druid-slow-sql.log 
log4j.appender.druid.File=D:/temp/log/druid-slow-sql.log

 

setSlowSqlMillis设小一点,试跑一下

老应用转druid并启用慢sql记录功能_第1张图片

你可能感兴趣的:(Web,java)