spring配置支持数据库连接池

背景:PAAS容器化后,使用spring jdbc配置,不支持数据库连接池,对查询和处理效率产生影响。

分析:原CloudSOP使用编码方式,实际datasource使用org.apache.commons.dbcp2.BasicDataSource,spring配置支持注入所需参数。所以,直接配置就可以解决问题。

以metric服务为例,配置如下:

<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxTotal" value="20"/>
<property name="maxIdle" value="10"/>
<property name="minIdle" value="0"/>
<property name="maxWaitMillis" value="10000"/>
<property name="testOnBorrow" value="true"/>
<property name="testOnReturn" value="true"/>
<property name="testWhileIdle" value="true"/>
<property name="numTestsPerEvictionRun" value="3"/>
<property name="minEvictableIdleTimeMillis" value="1800000"/>
<property name="timeBetweenEvictionRunsMillis" value="1800000"/>
<property name="validationQuery" value="SELECT 1 FROM DUAL"/>
<property name="validationQueryTimeout" value="5000"/>
<property name="removeAbandonedOnBorrow" value="true"/>
<property name="removeAbandonedTimeout" value="600"/>
<property name="connectionProperties"><value>connectTimeout=10000;socketTimeout=30000</value></property>
</bean>

 

替换掉原来的org.springframework.jdbc.datasource.AbstractDriverBasedDataSource 实现类。

实际业务中,变化的只有maxTotal 最大连接数 和 maxIdle 最大空闲连接数。如果有特殊需求,可以参考dbcp2配置指南。原CloudSOP datasource.cfg 配置的参数基本覆盖。

经测试,基本功能正常,查询效率提升1倍。

posted on 2018-01-27 14:53  yaoyu  阅读(233)  评论(0编辑  收藏  举报

导航