亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

數(shù)據(jù)庫(kù)阿里連接池 druid配置詳解

 更新時(shí)間:2017年05月02日 16:09:36   作者:Joker_Ye  
本篇文章主要介紹了數(shù)據(jù)庫(kù)阿里連接池 druid配置詳解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

Java程序很大一部分要操作數(shù)據(jù)庫(kù),為了提高性能操作數(shù)據(jù)庫(kù)的時(shí)候,有不得不使用數(shù)據(jù)庫(kù)連接池。數(shù)據(jù)庫(kù)連接池有很多選擇,c3p、dhcp、proxool等,druid作為一名后起之秀,憑借其出色的性能,也逐漸印入了大家的眼簾。接下來(lái)本教程就說(shuō)一下druid的簡(jiǎn)單使用。

首先從 http://repo1.maven.org/maven2/com/alibaba/druid/ 下載最新的jar包。如果想使用最新的源碼編譯,可以從 https://github.com/alibaba/druid 下載源碼,然后使用maven命令行,或者導(dǎo)入到eclipse中進(jìn)行編譯。

和dbcp類(lèi)似,druid的配置項(xiàng)如下

配置 缺省值 說(shuō)明
name   配置這個(gè)屬性的意義在于,如果存在多個(gè)數(shù)據(jù)源,監(jiān)控的時(shí)候 
可以通過(guò)名字來(lái)區(qū)分開(kāi)來(lái)。如果沒(méi)有配置,將會(huì)生成一個(gè)名字, 
格式是:"DataSource-" + System.identityHashCode(this)
jdbcUrl   連接數(shù)據(jù)庫(kù)的url,不同數(shù)據(jù)庫(kù)不一樣。例如: 
mysql : jdbc:mysql://10.20.153.104:3306/druid2  
oracle : jdbc:oracle:thin:@10.20.149.85:1521:ocnauto
username   連接數(shù)據(jù)庫(kù)的用戶名
password   連接數(shù)據(jù)庫(kù)的密碼。如果你不希望密碼直接寫(xiě)在配置文件中, 
可以使用ConfigFilter。詳細(xì)看這里: 
https://github.com/alibaba/druid/wiki/%E4%BD%BF%E7%94%A8ConfigFilter
driverClassName 根據(jù)url自動(dòng)識(shí)別 這一項(xiàng)可配可不配,如果不配置druid會(huì)根據(jù)url自動(dòng)識(shí)別dbType,然后選擇相應(yīng)的driverClassName
initialSize 0 初始化時(shí)建立物理連接的個(gè)數(shù)。初始化發(fā)生在顯示調(diào)用init方法,或者第一次getConnection時(shí)
maxActive 8 最大連接池?cái)?shù)量
maxIdle 8 已經(jīng)不再使用,配置了也沒(méi)效果
minIdle   最小連接池?cái)?shù)量
maxWait   獲取連接時(shí)最大等待時(shí)間,單位毫秒。配置了maxWait之后, 
缺省啟用公平鎖,并發(fā)效率會(huì)有所下降, 
如果需要可以通過(guò)配置useUnfairLock屬性為true使用非公平鎖。
poolPreparedStatements false 是否緩存preparedStatement,也就是PSCache。 
PSCache對(duì)支持游標(biāo)的數(shù)據(jù)庫(kù)性能提升巨大,比如說(shuō)oracle。 
在mysql5.5以下的版本中沒(méi)有PSCache功能,建議關(guān)閉掉。
作者在5.5版本中使用PSCache,通過(guò)監(jiān)控界面發(fā)現(xiàn)PSCache有緩存命中率記錄, 
該應(yīng)該是支持PSCache。
maxOpenPreparedStatements -1 要啟用PSCache,必須配置大于0,當(dāng)大于0時(shí), 
poolPreparedStatements自動(dòng)觸發(fā)修改為true。 
在Druid中,不會(huì)存在Oracle下PSCache占用內(nèi)存過(guò)多的問(wèn)題, 
可以把這個(gè)數(shù)值配置大一些,比如說(shuō)100
validationQuery   用來(lái)檢測(cè)連接是否有效的sql,要求是一個(gè)查詢(xún)語(yǔ)句。 
如果validationQuery為null,testOnBorrow、testOnReturn、 
testWhileIdle都不會(huì)其作用。
testOnBorrow true 申請(qǐng)連接時(shí)執(zhí)行validationQuery檢測(cè)連接是否有效,做了這個(gè)配置會(huì)降低性能。
testOnReturn false 歸還連接時(shí)執(zhí)行validationQuery檢測(cè)連接是否有效,做了這個(gè)配置會(huì)降低性能
testWhileIdle false 建議配置為true,不影響性能,并且保證安全性。 
申請(qǐng)連接的時(shí)候檢測(cè),如果空閑時(shí)間大于 
timeBetweenEvictionRunsMillis, 
執(zhí)行validationQuery檢測(cè)連接是否有效。
timeBetweenEvictionRunsMillis   有兩個(gè)含義: 
1) Destroy線程會(huì)檢測(cè)連接的間隔時(shí)間 
2) testWhileIdle的判斷依據(jù),詳細(xì)看testWhileIdle屬性的說(shuō)明
numTestsPerEvictionRun   不再使用,一個(gè)DruidDataSource只支持一個(gè)EvictionRun
minEvictableIdleTimeMillis    
connectionInitSqls   物理連接初始化的時(shí)候執(zhí)行的sql
exceptionSorter 根據(jù)dbType自動(dòng)識(shí)別 當(dāng)數(shù)據(jù)庫(kù)拋出一些不可恢復(fù)的異常時(shí),拋棄連接
filters   屬性類(lèi)型是字符串,通過(guò)別名的方式配置擴(kuò)展插件, 
常用的插件有: 
監(jiān)控統(tǒng)計(jì)用的filter:stat  
日志用的filter:log4j 
防御sql注入的filter:wall
proxyFilters   類(lèi)型是List<com.alibaba.druid.filter.Filter>, 
如果同時(shí)配置了filters和proxyFilters, 
是組合關(guān)系,并非替換關(guān)系

表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 數(shù)據(jù)庫(kù)連接池 --> 
 < bean id = "dataSource" class = "com.alibaba.druid.pool.DruidDataSource"destroy-method = "close" >  
    <!-- 數(shù)據(jù)庫(kù)基本信息配置 --> 
    < 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}" />  
    <!-- 最大并發(fā)連接數(shù) --> 
    < property name = "maxActive" value = "${maxActive}" /> 
    <!-- 初始化連接數(shù)量 --> 
    < property name = "initialSize" value = "${initialSize}" /> 
    <!-- 配置獲取連接等待超時(shí)的時(shí)間 --> 
    < property name = "maxWait" value = "${maxWait}" /> 
    <!-- 最小空閑連接數(shù) --> 
    < property name = "minIdle" value = "${minIdle}" />  
    <!-- 配置間隔多久才進(jìn)行一次檢測(cè),檢測(cè)需要關(guān)閉的空閑連接,單位是毫秒 --> 
    < property name = "timeBetweenEvictionRunsMillis" value ="${timeBetweenEvictionRunsMillis}" /> 
    <!-- 配置一個(gè)連接在池中最小生存的時(shí)間,單位是毫秒 --> 
    < 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}" /> 
    <!-- 打開(kāi) removeAbandoned 功能 --> 
    < property name = "removeAbandoned" value = "${removeAbandoned}" /> 
    <!-- 1800 秒,也就是 30 分鐘 --> 
    < property name = "removeAbandonedTimeout" value ="${removeAbandonedTimeout}" /> 
    <!-- 關(guān)閉 abanded 連接時(shí)輸出錯(cuò)誤日志 -->   
    < 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 監(jiān)控統(tǒng)計(jì)功能  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 監(jiān)控統(tǒng)計(jì)功能  end--> 

訪問(wèn)監(jiān)控頁(yè)面: http://ip:port/projectName/druid/index.html

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論