首页 > 数据库 > MySQL > 正文

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

2024-07-24 13:00:15
字体:
来源:转载
供稿:网友

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

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

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

表1.1 配置属性

加入 druid-1.0.9.jar

applicationContext.xml

< 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 >   ApplicationContext.xml配置druid  

<!-- 阿里 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}" />         <!-- 打开 removeAbandoned 功能 -->         < property name = "removeAbandoned" value = "${removeAbandoned}" />         <!-- 1800 秒,也就是 30 分钟 -->         < property name = "removeAbandonedTimeout" value ="${removeAbandonedTimeout}" />         <!-- 关闭 abanded 连接时输出错误日志 -->            < property name = "logAbandoned" value = "${logAbandoned}" />    </ bean >  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
<!-- 连接池 启用 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--> 访问监控页面: http://ip:port/projectName/druid/index.html


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表