一 整合阿里巴巴Druid
测试环境: mysql5.5 druid-1.0.9.jar
编写druid.properties
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/studentdb
theUser=root
password=test123
#配置监控统计拦截的filters,去掉后监控界面sql无法统计
filters=stat
#配置初始化大小
initialSize=6
#配置初始化最大连接数
maxActive=20
#配置初始化最小连接数
minIdle=3
#配置获取连接等待超时的时间,1分钟
maxWait=60000
#检测连接是否有效的SQL
validationQuery=SELECT 'x'
#空闲的连接是否进行有效性检查
testWhileIdle=true
#申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能
testOnBorrow=false
#归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能
testOnReturn=false
#启用PSCache,必须配置大于0,当大于0时,poolPreparedStatements自动触发修改为true
maxPoolPreparedStatementPerConnectionSize=20
#对于长时间不使用的连接强制关闭
removeAbandoned=true
#超过60秒的空闲连接就可以被关闭了,单位是秒
removeAbandonedTimeout=60
#配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
timeBetweenEvictionRunsMillis=10000
#配置一个连接在池中最小生存的时间,单位是毫秒
minEvictableIdleTimeMillis=30000
在spring中集成
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- 导入其它sping配置文件 -->
<import resource="classpath:configs/spring/context-*.xml"/>
<!-- durid连接池配置start -->
<bean id="duridConfig" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:configs/druid.properties</value>
</list>
</property>
</bean>
<!--druid数据源-->
<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url}" />
<property name="username" value="${theUser}" />
<property name="password" value="${password}" />
<property name="filters" value="${filters}" />
<property name="initialSize" value="${initialSize}" />
<property name="maxActive" value="${maxActive}