Druid是阿里开源的一个为监控而生的数据库连接池,如果项目需要查看性能使用这个将可以非常清晰的看到大部分监控项目。项目地址是https://github.com/alibaba/druid
Druid是Java语言中最好的数据库连接池。Druid能够提供强大的监控和扩展功能。首先编写druid的配置文件
[code lang=”js”]
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/bobodb?createDatabaseIfNotExist=true&autoReconnect=true&useUnicode=true&characterEncoding=utf-8&mysqlEncoding=utf8
username=xxxx
password=xxxxxxxx
filters=stat
initialSize=2
maxActive=300
maxWait=60000
timeBetweenEvictionRunsMillis=60000
minEvictableIdleTimeMillis=300000
validationQuery=SELECT 1
testWhileIdle=true
testOnBorrow=false
testOnReturn=false
poolPreparedStatements=false
maxPoolPreparedStatementPerConnectionSize=200
connectionProperties=druid.stat.mergeSql\=true
[/code]
然后创建连接池
[code lang=”js”]
static {
try {
Properties properties = new Properties();
properties.load(DBConnection.class.getClassLoader().getResourceAsStream("druid.properties"));
dataSource = DruidDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
logger.error("初始化数据库连接异常:{}", e.getMessage()); //$NON-NLS-1$
e.printStackTrace();
}
}
[/code]
获取连接
[code lang=”js”]
Connection conn = null;
try {
conn = dataSource.getConnection();
} catch (Exception e) {
logger.error("获取Mysql数据库连接异常:{}", e.getMessage()); //$NON-NLS-1$
e.printStackTrace();
}
[/code]
配置url监控(web.xml)
[code lang=”js”]
DruidStatView
com.alibaba.druid.support.http.StatViewServlet
DruidStatView
/druid/*
DruidWebStatFilter
com.alibaba.druid.support.http.WebStatFilter
exclusions
*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*
DruidWebStatFilter
/*
[/code]
其他更详细的配置可以参照官网文档
https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98
打开http://localhost:8080/QLBoboService/druid/sql.html可以看到效果
–每天一点点