Druid连接池简单配置

  Druid是阿里巴巴开源平台上的一个项目,整个项目由数据库连接池、插件框架和SQL解析器组成。该项目主要是为了扩展JDBC的一些限制,可以让程序员实现一些特殊的需求,比如向密钥服务请求凭证、统计SQL信息、SQL性能收集、SQL注入检查、SQL翻译等,程序员可以通过定制来实现自己需要的功能。

  1、pom里面添加相关的dependency

 1         <dependency>
 2             <groupId>com.alibabagroupId>
 3             <artifactId>druidartifactId>
 4             <version>0.2.25version>
 5         dependency>
 6         <dependency>
 7             <groupId>com.alibabagroupId>
 8             <artifactId>dubboartifactId>
 9             <version>2.5.3version>
10         dependency>

  2、在spring 配置文件application-context.xml里面配置dataSource

 1 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
 2         <property name="driverClassName" value="${jdbc.driver}"/>
 3         <property name="url" value="${jdbc.url}"/>
 4         <property name="username" value="${jdbc.username}"/>
 5         <property name="password" value="${jdbc.password}"/>
 6         <property name="dbType" value="mysql"/>
 7         <property name="initialSize" value="${jdbc.initialSize}"/>
 8         <property name="minIdle" value="${jdbc.minIdle}"/>
 9         <property name="maxActive" value="${jdbc.maxActive}"/>
10         <property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}"/>
11         <property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}"/>
12         <property name="validationQuery" value="${jdbc.validationQuery}"/>
13         <property name="testWhileIdle" value="${jdbc.testWhileIdle}"/>
14         <property name="testOnBorrow" value="${jdbc.testOnBorrow}"/>
15         <property name="testOnReturn" value="${jdbc.testOnReturn}"/>
16         <property name="maxOpenPreparedStatements" value="${jdbc.maxOpenPreparedStatements}"/>
17         <property name="removeAbandoned" value="${jdbc.removeAbandoned}"/>
18         <property name="removeAbandonedTimeout" value="${jdbc.removeAbandonedTimeout}"/>
19         <property name="logAbandoned" value="${jdbc.logAbandoned}"/>
20         <property name="filters" value="${jdbc.filtes}"/>
21 bean>

  其中 init-method="init" 初始化的时候加载该dataSource;driverClassName也可以省略,因为druid可以根据url识别其driver;在识别特定的数据库时需要特别指定dbType类型(例如:在连接Sqlite是,其类型要写成mysql);

  3、properties文件

jdbc.url=jdbc:mysql://127.0.0.1:3306/upp?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=root
jdbc.initialSize=1
jdbc.minIdle=1
jdbc.maxActive=20
jdbc.timeBetweenEvictionRunsMillis=60000
jdbc.minEvictableIdleTimeMillis=300000
jdbc.validationQuery=SELECT 'x'
jdbc.testWhileIdle=true
jdbc.testOnBorrow=false
jdbc.testOnReturn=false
jdbc.maxOpenPreparedStatements=-1
jdbc.removeAbandoned=true
jdbc.removeAbandonedTimeout=1800
jdbc.logAbandoned=true
jdbc.filtes=wall,stat

  一般情况只需要修改url、username、password即可。

  4、web.xml配置

 1 <filter>
 2         <filter-name>DruidWebStatFilterfilter-name>
 3         <filter-class>com.alibaba.druid.support.http.WebStatFilterfilter-class>
 4         <init-param>
 5             <param-name>exclusionsparam-name>
 6             <param-value>*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*param-value>
 7         init-param>
 8     filter>
 9     <filter-mapping>
10         <filter-name>DruidWebStatFilterfilter-name>
11         <url-pattern>/*url-pattern>
12     filter-mapping>
13     <servlet>
14         <servlet-name>DruidStatViewservlet-name>
15         <servlet-class>com.alibaba.druid.support.http.StatViewServletservlet-class>
16     servlet>
17     <servlet-mapping>
18         <servlet-name>DruidStatViewservlet-name>
19         <url-pattern>/druid/*url-pattern>
20 servlet-mapping>

如果在DruidStatView里添加下面的参数可以为监控页面设置账号密码:

 1 <servlet>
 2         <servlet-name>DruidStatViewservlet-name>
 3          <servlet-class>com.alibaba.druid.support.http.StatViewServletservlet-class>
 4          <init-param>
 5             <param-name>resetEnableparam-name>
 6             <param-value>falseparam-value>
 7         init-param>
 8         <init-param>
 9             
10             <param-name>loginUsernameparam-name>
11             <param-value>usernameparam-value>
12         init-param>
13         <init-param>
14             
15             <param-name>loginPasswordparam-name>
16             <param-value>passwordparam-value>
17         init-param>
18 servlet>

 

  5、可以通过web页面访问监控页面

Druid连接池简单配置_第1张图片

更详细的内容可以参考 http://www.cnblogs.com/niejunlei/p/5977895.html。

谢谢大家!

你可能感兴趣的:(Druid连接池简单配置)