阿里巴巴推出的国产数据库连接池,据网上测试对比,比目前的DBCP或C3P0数据库连接池性能更好,Druid与其他数据库连接池使用方法基本一样(与DBCP非常相似),将数据库的连接信息全部配置给DataSource对象。
基于纯Java代码使用:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
package
demo.test;
import
java.io.InputStream;
import
java.sql.Connection;
import
java.sql.SQLException;
import
java.util.Properties;
import
javax.sql.DataSource;
import
com.alibaba.druid.pool.DruidDataSourceFactory;
public
class
DBUtil {
private
static
DataSource ds =
null
;
static
{
try
{
InputStream in = DBUtil.
class
.getClassLoader()
.getResourceAsStream(
"ds.properties"
);
Properties props =
new
Properties();
props.load(in);
ds = DruidDataSourceFactory.createDataSource(props);
}
catch
(Exception ex){
ex.printStackTrace();
}
}
public
static
Connection openConnection()
throws
SQLException{
return
ds.getConnection();
}
}
|
ds.properties内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
driverClassName = oracle.jdbc.driver.OracleDriver
url = jdbc:oracle:thin:
@127
.0.
0.1
:
1521
:ORCL
username = ******
password = ******
initialSize =
5
maxActive =
10
minIdle =
3
maxWait =
60000
removeAbandoned =
true
removeAbandonedTimeout =
180
timeBetweenEvictionRunsMillis =
60000
minEvictableIdleTimeMillis =
300000
validationQuery = SELECT
1
FROM DUAL
testWhileIdle =
true
testOnBorrow =
false
testOnReturn =
false
poolPreparedStatements =
true
maxPoolPreparedStatementPerConnectionSize =
50
filters = stat
|
基于Spring的配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
<
bean
id
=
"druidDataSource"
class
=
"com.alibaba.druid.pool.DruidDataSource"
init-method
=
"init"
destroy-method
=
"close"
>
<!-- 数据库基本信息配置 -->
<
property
name
=
"driverClassName"
value
=
"${oracle.driver}"
/>
<
property
name
=
"url"
value
=
"${oracle.url}"
/>
<
property
name
=
"username"
value
=
"${oracle.username}"
/>
<
property
name
=
"password"
value
=
"${oracle.password}"
/>
<!-- 初始化连接数量 -->
<
property
name
=
"initialSize"
value
=
"${druid.initialSize}"
/>
<!-- 最大并发连接数 -->
<
property
name
=
"maxActive"
value
=
"${druid.maxActive}"
/>
<!-- 最大空闲连接数 -->
<
property
name
=
"maxIdle"
value
=
"${druid.maxIdle}"
/>
<!-- 最小空闲连接数 -->
<
property
name
=
"minIdle"
value
=
"${druid.minIdle}"
/>
<!-- 配置获取连接等待超时的时间 -->
<
property
name
=
"maxWait"
value
=
"${druid.maxWait}"
/>
<!-- 超过时间限制是否回收 -->
<
property
name
=
"removeAbandoned"
value
=
"${druid.removeAbandoned}"
/>
<!-- 超过时间限制多长; -->
<
property
name
=
"removeAbandonedTimeout"
value
=
"${druid.removeAbandonedTimeout}"
/>
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<
property
name
=
"timeBetweenEvictionRunsMillis"
value
=
"${druid.timeBetweenEvictionRunsMillis}"
/>
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<
property
name
=
"minEvictableIdleTimeMillis"
value
=
"${druid.minEvictableIdleTimeMillis}"
/>
<!-- 用来检测连接是否有效的sql,要求是一个查询语句-->
<
property
name
=
"validationQuery"
value
=
"${druid.validationQuery}"
/>
<!-- 申请连接的时候检测 -->
<
property
name
=
"testWhileIdle"
value
=
"${druid.testWhileIdle}"
/>
<!-- 申请连接时执行validationQuery检测连接是否有效,配置为true会降低性能 -->
<
property
name
=
"testOnBorrow"
value
=
"${druid.testOnBorrow}"
/>
<!-- 归还连接时执行validationQuery检测连接是否有效,配置为true会降低性能 -->
<
property
name
=
"testOnReturn"
value
=
"${druid.testOnReturn}"
/>
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
<
property
name
=
"poolPreparedStatements"
value
=
"${druid.poolPreparedStatements}"
/>
<
property
name
=
"maxPoolPreparedStatementPerConnectionSize"
value
=
"${druid.maxPoolPreparedStatementPerConnectionSize}"
/>
<!--属性类型是字符串,通过别名的方式配置扩展插件,常用的插件有:
监控统计用的filter:stat
日志用的filter:log4j
防御SQL注入的filter:wall -->
<
property
name
=
"filters"
value
=
"${druid.filters}"
/>
</
bean
>
|
启用Web监控统计功能需要在Web应用的web.xml中加入以下内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<
filter
>
<
filter-name
>DruidWebStatFilter</
filter-name
>
<
filter-class
>com.alibaba.druid.support.http.WebStatFilter</
filter-class
>
<
init-param
>
<
param-name
>exclusions</
param-name
>
<
param-value
>*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*</
param-value
>
</
init-param
>
</
filter
>
<
filter-mapping
>
<
filter-name
>DruidWebStatFilter</
filter-name
>
<
url-pattern
>/*</
url-pattern
>
</
filter-mapping
>
<
servlet
>
<
servlet-name
>DruidStatView</
servlet-name
>
<
servlet-class
>com.alibaba.druid.support.http.StatViewServlet</
servlet-class
>
</
servlet
>
<
servlet-mapping
>
<
servlet-name
>DruidStatView</
servlet-name
>
<
url-pattern
>/druid/*</
url-pattern
>
</
servlet-mapping
>
|
访问监控页面:http://ip:port/projectName/druid/index.html