
removeAbandonedTimeout:1800 27.
#将当前关闭动作记录到日志 28.
logAbandoned:true 29.
配置文件1.1
配置项中指定了各个参数后,在连接池内部是这么使用这些参数的。数据库连接池在初始化的时候会创建initialSize个连接,当有数据库操作时,会从
池中取出一个连接。如果当前池中正在使用的连接数等于maxActive,则会等待一段时间,等待其他操作释放掉某一个连接,如果这个等待时间超过了
maxWait,则会报错;如果当前正在使用的连接数没有达到maxActive,则判断当前是否空闲连接,如果有则直接使用空闲连接,如果没有则新建立一
个连接。在连接使用完毕后,不是将其物理连接关闭,而是将其放入池中等待其他操作复用。
同时连接池内部有机制判断,如果当前的总的连接数少于miniIdle,则会建立新的空闲连接,以保证连接数得到miniIdle。如果当前连接池中某个连
接在空闲了timeBetweenEvictionRunsMillis时间后任然没有使用,则被物理性的关闭掉。有些数据库连接的时候有超时限制(mysql连接在8小时后断
开),或者由于网络中断等原因,连接池的连接会出现失效的情况,这时候设置一个testWhileIdle参数为true,可以保证连接池内部定时检测连接的
可用性,不可用的连接会被抛弃或者重建,最大情况的保证从连接池中得到的Connection对象是可用的。当然,为了保证绝对的可用性,你也可以使
用testOnBorrow为true(即在获取Connection对象时检测其可用性),不过这样会影响性能。
2 代码编写
2.1 使用spring (applicationContext.xml)
首先给出spring配置文件
<?xml version="1.0" encoding="UTF-8"?> 01.
<beans xmlns="http://www.springframework.org/schema/beans" 02.
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 03.
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 04.
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 05.
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 06.
<!-- 给web使用的spring文件 --> 07.
<bean id="propertyConfigurer" 08.
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 09.
<property name="locations"> 10.
<list> 11.
<value>/WEB-INF/classes/dbconfig.properties</value> 12.
</list> 13.
</property> 14.
</bean> 15.
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" 16.
destroy-method="close"> 17.
<property name="url" value="${url}" /> 18.
<property name="username" value="${username}" /> 19.
<property name="password" value="${password}" /> 20.
<property name="driverClassName" value="${driverClassName}" /> 21.
<property name="filters" value="${filters}" /> 22.
23.
<property name="maxActive" value="${maxActive}" /> 24.
<property name="initialSize" value="${initialSize}" /> 25.
<property name="maxWait" value="${maxWait}" /> 26.
<property name="minIdle" value="${minIdle}" /> 27.
28.
<property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}" /> 29.
<property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}" /> 30.
31.
<property name="validationQuery" value="${validationQuery}" /> 32.
<property name="testWhileIdle" value="${testWhileIdle}" /> 33.
<property name="testOnBorrow" value="${testOnBorrow}" /> 34.
<property name="testOnReturn" value="${testOnReturn}" /> 35.
<property name="maxOpenPreparedStatements" 36.
value="${maxOpenPreparedStatements}" /> 37.
<property name="removeAbandoned" value="${removeAbandoned}" /> <!-- 打开removeAbandoned功能 --> 38.
<property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}" /> <!-- 1800秒,也就是30分钟 --> 39.
<property name="logAbandoned" value="${logAbandoned}" /> <!-- 关闭abanded连接时输出错误日志 --> 40.
</bean> 41.
42.
<bean id="dataSourceDbcp" class="org.apache.commons.dbcp.BasicDataSource" 43.
destroy-method="close"> 44.
45.
<property name="driverClassName" value="${driverClassName}" /> 46.
<property name="url" value="${url}" /> 47.
<property name="username" value="${username}" /> 48.
<property name="password" value="${password}" /> 49.
50.