mysql性能优化(七) 数据库阿里连接池 druid配置详解

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

首先从 http://repo1.maven.org/maven2/com/alibaba/druid/ 下载最新的jar包。如果想使用最新的源码编译,可以从 https://github.com/alibaba/druid 下载源码,然后使用maven命令行,或者导入到eclipse中进行编译。

和dbcp类似,druid的配置项如下

mysql性能优化(七) 数据库阿里连接池 druid配置详解_第1张图片mysql性能优化(七) 数据库阿里连接池 druid配置详解_第2张图片mysql性能优化(七) 数据库阿里连接池 druid配置详解_第3张图片

表1.1 配置属性

加入 druid-1.0.9.jar

ApplicationContext.xml

< bean name = "transactionManager" class ="org.springframework.jdbc.datasource.DataSourceTransactionManager" >     
    < property name = "dataSource" ref = "dataSource" >  
       
    < bean id = "propertyConfigurer" class ="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >    
       < property name = "locations" >    
           < list >    
                 < value > /WEB-INF/classes/dbconfig.properties     
                
            
      
 ApplicationContext.xml配置druid   

  
  < bean id = "dataSource" class = "com.alibaba.druid.pool.DruidDataSource"destroy-method = "close" >    
         
       < property name = "url" value = "${url}" />    
       < property name = "username" value = "${username}" />    
       < property name = "password" value = "${password}" />    
       < property name = "driverClassName" value = "${driverClassName}" />    
       < property name = "filters" value = "${filters}" />    
          
       < property name = "maxActive" value = "${maxActive}" />  
         
       < property name = "initialSize" value = "${initialSize}" />  
         
       < property name = "maxWait" value = "${maxWait}" />  
         
       < property name = "minIdle" value = "${minIdle}" />    
         
       < property name = "timeBetweenEvictionRunsMillis" value ="${timeBetweenEvictionRunsMillis}" />  
         
       < property name = "minEvictableIdleTimeMillis" value ="${minEvictableIdleTimeMillis}" />    
       < property name = "validationQuery" value = "${validationQuery}" />    
       < property name = "testWhileIdle" value = "${testWhileIdle}" />    
       < property name = "testOnBorrow" value = "${testOnBorrow}" />    
       < property name = "testOnReturn" value = "${testOnReturn}" />    
       < property name = "maxOpenPreparedStatements" value ="${maxOpenPreparedStatements}" />  
         
       < property name = "removeAbandoned" value = "${removeAbandoned}" />  
         
       < property name = "removeAbandonedTimeout" value ="${removeAbandonedTimeout}" />  
            
       < property name = "logAbandoned" value = "${logAbandoned}" />  
    
dbconfig.properties

url: jdbc:mysql:// localhost :3306/ newm  
driverClassName: com.mysql.jdbc.Driver  
username: root  
password: root  
filters: stat  
maxActive: 20  
initialSize: 1  
maxWait: 60000  
minIdle: 10  
maxIdle: 15  
timeBetweenEvictionRunsMillis: 60000  
minEvictableIdleTimeMillis: 300000  
validationQuery: SELECT 'x'  
testWhileIdle: true  
testOnBorrow: false  
testOnReturn: false  
maxOpenPreparedStatements: 20  
removeAbandoned: true  
removeAbandonedTimeout: 1800  
logAbandoned: true 
web.xml
  
  < filter >  
     < filter-name > DruidWebStatFilter   
     < filter-class > com.alibaba.druid.support.http.WebStatFilter   
     < init-param >  
         < param-name > exclusions   
         < param-value > *. js ,*. gif ,*. jpg ,*. png ,*. css ,*. ico ,/ druid /*   
       
    
  < filter-mapping >  
     < filter-name > DruidWebStatFilter   
     < url-pattern > /*   
    
  < servlet >  
     < servlet-name > DruidStatView   
     < servlet-class > com.alibaba.druid.support.http.StatViewServlet   
    
  < servlet-mapping >  
     < servlet-name > DruidStatView   
     < url-pattern > / druid /*   
    
   
访问监控页面: http://ip:port/projectName/druid/index.html




你可能感兴趣的:(数据库-mysql)