使用阿里巴巴開源的Druid配置數據庫鏈接池。在這一樣記錄下測試配置過程。整體來講,druid的配置文件和dbcp是很是相似的。java
項目部分引用jar的maven配置:mysql
<!-- druid --> <dependency> <groupId>commons-pool</groupId> <artifactId>commons-pool</artifactId> <version>1.6</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.6</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency>
一、新建簡單spring項目。spring
項目目錄說明:sql
druidTestfileBeans.xml: spring應用上下文。數據庫
jdbc.properties:druid數據庫鏈接池配置文件。緩存
UserDao.java:DAO層bean。bash
MainDruid.java:測試類。oracle
測試用數據庫表及表數據以下:maven
二、代碼性能
jdbc.properties:
#數據庫驅動 jdbc.driverClassName=com.mysql.jdbc.Driver #數據庫 jdbc.url=jdbc:mysql://172.21.4.45:3307/cdspdb?useUnicode=true&characterEncoding=utf-8 #用戶名 jdbc.username=cdsp #密碼 jdbc.password=cdsp #別名方式,擴展插件,監控統計用的filter:stat,日誌用的filter:log4j,防護sql注入的filter:wall jdbc.filters=stat #最大鏈接數 jdbc.maxActive=300 #初始化鏈接數 jdbc.initialSize=2 #獲取鏈接最大等待時間 jdbc.maxWait=60000 #最小鏈接數 jdbc.minIdle=1 #檢測鏈接有效性的時間間隔 jdbc.timeBetweenEvictionRunsMillis=60000 #鏈接保持空閒而不被驅逐的最長時間 jdbc.minEvictableIdleTimeMillis=300000 #鏈接有效性,檢測sql jdbc.validationQuery=SELECT 'x' #定時檢測空閒鏈接有效性 jdbc.testWhileIdle=true #檢測獲取的鏈接的有效性 jdbc.testOnBorrow=false #檢測要歸還的鏈接的有效性 jdbc.testOnReturn=false #是否緩存preparedStatement,即PSCache。PSCache對支持遊標的數據庫性能提高巨大,好比說oracle。在mysql下建議關閉。 jdbc.poolPreparedStatements=false jdbc.maxPoolPreparedStatementPerConnectionSize=50
druidTestfileBeans.xml:
<?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- jdbc配置文件 --> <context:property-placeholder location="com/springtest/druid/jdbc.properties" /> <!-- 配置dbcp數據源鏈接池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="filters" value="${jdbc.filters}" /> <property name="maxActive" value="${jdbc.maxActive}" /> <property name="initialSize" value="${jdbc.initialSize}" /> <property name="maxWait" value="${jdbc.maxWait}" /> <property name="minIdle" value="${jdbc.minIdle}" /> <property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}" /> <property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}" /> <property name="validationQuery" value="${jdbc.validationQuery}" /> <property name="testWhileIdle" value="${jdbc.testWhileIdle}" /> <property name="testOnBorrow" value="${jdbc.testOnBorrow}" /> <property name="testOnReturn" value="${jdbc.testOnReturn}" /> <property name="poolPreparedStatements" value="${jdbc.poolPreparedStatements}" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="${jdbc.maxPoolPreparedStatementPerConnectionSize}" /> </bean> <!-- 引入spingjdbc --> <bean id="JdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <bean id="UserDao" class="com.springtest.dbcp.UserDao"> <property name="jdbcTemplate" ref="JdbcTemplate"></property> </bean> </beans>
UserDao.java:
package com.springtest.druid; import java.util.List; import java.util.Map; import javax.annotation.Resource; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; public class UserDao { JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public List<Map<String,Object>> queryByName(String name) { String sql = "select * from thnb_user where user_name='" + name + "' "; List<Map<String,Object>> list = jdbcTemplate.queryForList(sql); return list; } }
MainDruid.java:
package com.springtest.druid; import java.util.List; import java.util.Map; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.springtest.dbcp.UserDao; public class MainDruid { public static void main(String[] args) { String contextXmlPath2 = "com/springtest/druid/druidTestfileBeans.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(contextXmlPath2); UserDao userDao = (UserDao) ctx.getBean("UserDao"); List<Map<String,Object>> list = userDao.queryByName("thb"); System.out.println(list); } }
四、測試驗證
執行MainDruid.java,打印以下: