今天在网上无意间看到了Druid连接池(http://code.alibabatech.com/wiki/display/Druid/Home),正好闲来无事,在我们项目中配置了一下,特此记录一下。 我们项目用的SSH框架,数据库是Oracle,使用的是proxool连接池,所以改成druid还是很简单的。
-
首先下载druid的jar包,我用的是0.2.9
-
在src下添加druid.properties,内容如下: url:jdbc:oracle:thin:@localhost:1521:orcl driverClassName:oracle.jdbc.driver.OracleDriver username:username password:password
filters:stat maxActive:20
initialSize:1
maxWait:60000
minIdle:10
timeBetweenEvictionRunsMillis:60000
minEvictableIdleTimeMillis:300000
validationQuery:SELECT 'x'
testWhileIdle:true
testOnBorrow:false
testOnReturn:false
poolPreparedStatements:true
maxOpenPreparedStatements:20
removeAbandoned:true
removeAbandonedTimeout:1800
logAbandoned:true -
编辑Spring的配置文件applicationContext.xml,修改连接池为druid。首先通过spring加载druid.properties,然后修改连接池: <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>/WEB-INF/classes/druid.properties</value>
</list>
</property>
</bean> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> <property name="driverClassName" value="${driverClassName}" /> <property name="filters" value="${filters}" /><property name="maxActive" value="${maxActive}" /> <property name="initialSize" value="${initialSize}" /> <property name="maxWait" value="${maxWait}" /> <property name="minIdle" value="${minIdle}" /> <property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}" /> <property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}" /> <property name="validationQuery" value="${validationQuery}" /> <property name="testWhileIdle" value="${testWhileIdle}" /> <property name="testOnBorrow" value="${testOnBorrow}" /> <property name="testOnReturn" value="${testOnReturn}" /> <property name="poolPreparedStatements" value="${poolPreparedStatements}" /> <property name="maxOpenPreparedStatements" value="${maxOpenPreparedStatements}" /> <property name="removeAbandoned" value="${removeAbandoned}" /> <!-- 打开removeAbandoned功能 --> <property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}" /> <!-- 1800秒,也就是30分钟 --> <property name="logAbandoned" value="${logAbandoned}" /> <!-- 关闭abanded连接时输出错误日志 -->
</bean>