zoukankan      html  css  js  c++  java
  • Druid

    简介

    DRUID是阿里巴巴开源平台上一个数据库连接池实现,它结合了C3P0、DBCP、PROXOOL等DB池的优点,同时加入了日志监控,可以很好的监控DB池连接和SQL的执行情况,可以说是针对监控而生的DB连接池

    maven依赖jar包

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.1.2</version>
    </dependency>
    

    在spring中的使用示例

    • jdbc.properties
    mysql.database.driverName=com.mysql.jdbc.Driver
    mysql.database.url=jdbc:mysql://127.0.0.1:3306/spring_security3?useUnicode=true&characterEncoding=utf8&useSSL=true&allowMultiQueries=true 
    mysql.database.user=root
    mysql.database.password=123456
    initialSize=1
    maxIdle=8
    minIdle=1
    maxActive=8
    maxWait=10000
    timeBetweenEvictionRunsMillis=60000
    minEvictableIdleTimeMillis=300000
    validationQuery=SELECT 1 FROM DUAL
    #最大分配的对象数
    default.max.total=1000
    #最大能够保持idel状态的对象数
    default.max.idle=200
    #当池内没有返回对象时,最大等待时间 1s
    default.max.wait=1000
    #当调用borrow Object方法时,是否进行有效性检查
    default.testonborrow=true
    #当调用return Object方法时,是否进行有效性检查
    default.testonreturn=true
    
    • dataSource配置applicationContext-dataSource.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:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
     	
     	  <!-- 配置数据源 -->
    	  <bean id="mysqlDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> 
    		  <property name="driverClassName" value="${mysql.database.driverName}" /> 
    		  <property name="url" value="${mysql.database.url}" /> 
    		  <property name="username" value="${mysql.database.user}" /> 
    		  <property name="password" value="${mysql.database.password}" /> 
    		  <!-- 配置初始化大小、最小、最大 --> 
    		  <!--初始化时建立物理连接的个数。初始化发生在显示调用init方法,或者第一次getConnection时 -->
    		  <property name="initialSize" value="${initialSize}" /> 
    		  <!-- 已经不再使用,配置了也没效果-->
    		  <property name="maxIdle" value="${maxIdle}" />
    		  <!--	最小连接池数量 -->
    		  <property name="minIdle" value="${minIdle}" /> 
    		  <!--最大连接池数量 -->
    		  <property name="maxActive" value="${maxActive}" />
    		
    		  <!-- 配置获取连接等待超时的时间 --> 
    		  <property name="maxWait" value="${maxWait}" />
    		
    		  <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> 
    		  <property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}" />
    		
    		  <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> 
    		  <property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}" />
    		
    		  <property name="testWhileIdle" value="true" />
    		
    		  <!-- 这里建议配置为TRUE,防止取到的连接不可用 --> 
    		  <property name="testOnBorrow" value="true" /> 
    		  <property name="testOnReturn" value="false" />
    		
    		  <!-- 打开PSCache,并且指定每个连接上PSCache的大小 --> 
    		  <property name="poolPreparedStatements" value="true" /> 
    		  <property name="maxPoolPreparedStatementPerConnectionSize" 
    		   value="20" />
    		
    		  <!-- 这里配置提交方式,默认就是TRUE,可以不用配置 -->
    		
    		  <property name="defaultAutoCommit" value="true" />
    		
    		  <!-- 验证连接有效与否的SQL,不同的数据配置不同 --> 
    		  <property name="validationQuery" value="${validationQuery}" /> 
    		  <property name="filters" value="stat" /> 
     
    </beans>
    
    
    • applicaionContext.xml中引入jdbc.properties和数据源applicationContext-dataSource.xml
    <!-- 引入jdbc文件 -->
    	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    		<property name="order" value="1"></property>
    		<property name="ignoreUnresolvablePlaceholders" value="true"></property>
    		<property name="locations">
    			<list>
                    <value>classpath*:dataSource.properties</value>
                </list>
    		</property>
    	</bean>
     
    	<!-- 导入dataSource文件 -->
        <import resource="classpath*:applicationContext-security.xml" />
    

    note : druid配置参数详解和durid对web和日志文件监控见引用文章
    引用:
    https://www.cnblogs.com/wuyun-blog/p/5679073.html

  • 相关阅读:
    从csv文件里取数据作为请求参数,和把返回数据放到一个csv文件
    记一次if控制器的使用
    记一次使用正则表达式+foreach控制器调试
    获取随机数用作入参使用
    获取返回结果作为参数并将其设置为全局变量(实现跨线程组使用)
    linux默认的目录结构
    总结fiddler抓https包
    Codeforces Round #733 (Div. 1 + Div. 2) D. Secret Santa
    Codeforces Round #733 (Div. 1 + Div. 2) C. Pursuit
    Codeforces Round #731 (Div. 3) A
  • 原文地址:https://www.cnblogs.com/nwu-edu/p/9420675.html
Copyright © 2011-2022 走看看