现在的位置: 首页 > 建站运维教程 > 正文

阿里数据库连接池druid配置详解

2023年01月20日 建站运维教程 ⁄ 共 3849字 ⁄ 字号 暂无评论

java程序很大一部分要操作数据库,为了提高性能操作数据库的时候,又不得不使用数据库连接池。数据库连接池有很多选择,c3p、dhcp、proxool等,druid作为一名后起之秀,凭借其出色的性能,也逐渐映入了大家的眼帘。接下来本教程就说一下druid的简单使用。

jar包下载、配置详解及配置示例

1. jar包获取

  • 可以从 http://repo1.maven.org/maven2/com/alibaba/druid/ 下载最新的jar包。
  • 如果想使用最新的源码编译,也可以从 https://github.com/alibaba/druid 下载源码,然后使用maven命令行,或者导入到IDEA中进行编译获得。
  • 从maven的仓库中获取
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.1.6</version>
    </dependency>

2. 配置示例

spring.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
    
    <bean name = "transactionManager" 
      class ="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
        <property name = "dataSource" ref = "dataSource" ></ property >
    </bean >

    <bean id = "propertyConfigurer" 
      class ="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
        <property name = "locations" >
            <list>
                <value > /WEB-INF/classes/dbconfig.properties </ value >
            </list>
        </property>
    </bean>

    <!-- 阿里 druid 数据库连接池 -->
    <bean id = "dataSource" class = "com.alibaba.druid.pool.DruidDataSource" destroy-method = "close" >
      <!-- 数据库基本信息配置 -->
      <property name = "url" value = "${jdbc.url}" />
      <property name = "username" value = "${jdbc.username}" />
      <property name = "password" value = "${jdbc.password}" />
      <property name = "driverClassName" value = "${jdbc.driverClassName}" />
      <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 = "maxOpenPreparedStatements" value ="${jdbc.maxOpenPreparedStatements}" />
    
      <!-- 打开 removeAbandoned 功能 -->
      <property name = "removeAbandoned" value = "${jdbc.removeAbandoned}" />
    
      <!-- 1800 秒,也就是 30 分钟 -->
      <property name = "removeAbandonedTimeout" value ="${jdbc.removeAbandonedTimeout}" />
    
      <!-- 关闭 abanded 连接时输出错误日志 -->
      <property name = "logAbandoned" value = "${jdbc.logAbandoned}" />
    </bean>
</beans>
jdbc.url=jdbc:mysql://localhost:3306/newm

jdbc.driverClassName=com.mysql.jdbc.Driver

jdbc.username=root

jdbc.password=root

jdbc.filters=stat

jdbc.maxActive=20

jdbc.initialSize=1

jdbc.maxWait=60000

jdbc.minIdle=10

jdbc.maxIdle=15

jdbc.timeBetweenEvictionRunsMillis=60000

jdbc.minEvictableIdleTimeMillis=300000

jdbc.validationQuery=SELECT 'x'

jdbc.testWhileIdle=true

jdbc.testOnBorrow=false

jdbc.testOnReturn=false

jdbc.maxOpenPreparedStatements=20

jdbc.removeAbandoned=true

jdbc.removeAbandonedTimeout=1800

jdbc.logAbandoned=true
<!-- 连接池 启用 Web 监控统计功能    start-->
<filter>
    <filter-name>DruidWebStatFilter</filter-name>
    <filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class>
    <init-param>
        <param-name>exclusions</param-name>
        <param-value>*.js ,*.gif ,*.jpg ,*.png ,*.css ,*.ico ,/druid/*</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>DruidWebStatFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<servlet>
    <servlet-name>DruidStatView</servlet-name>
    <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>DruidStatView</servlet-name>
    <url-pattern>/druid/*</url-pattern>
</servlet-mapping>
<!-- 连接池 启用 Web 监控统计功能    end-->

给我留言

您必须 [ 登录 ] 才能发表留言!