定義
數(shù)據(jù)庫連接是一種關(guān)鍵的有限的昂貴的資源,這一點(diǎn)在多用戶的網(wǎng)頁應(yīng)用程序中體現(xiàn)得尤為突出。對數(shù)據(jù)庫連接的管理能顯著影響到整個(gè)應(yīng)用程序的伸縮性和健壯性,影響到程序的性能指標(biāo)。數(shù)據(jù)庫連接池正是針對這個(gè)問題提出來的。
數(shù)據(jù)庫連接池負(fù)責(zé)分配、管理和釋放數(shù)據(jù)庫連接,它允許應(yīng)用程序重復(fù)使用一個(gè)現(xiàn)有的數(shù)據(jù)庫連接,而不是再重新建立一個(gè);釋放空閑時(shí)間超過最大空閑時(shí)間的數(shù)據(jù)庫連接來避免因?yàn)闆]有釋放數(shù)據(jù)庫連接而引起的數(shù)據(jù)庫連接遺漏。這項(xiàng)技術(shù)能明顯提高對數(shù)據(jù)庫操作的性能。
參考資料
DBCP
下載地址:http://commons.apache.org/proper/commons-dbcp/download_dbcp.cgi
相關(guān)屬性說明: http://commons.apache.org/proper/commons-dbcp/configuration.html
c3p0
下載地址:http://sourceforge.net/projects/c3p0/
相關(guān)屬性說明: http://www.mchange.com/projects/c3p0/
Druid
下載地址:http://repo1.maven.org/maven2/com/alibaba/druid/
相關(guān)屬性說明: https://github.com/alibaba/druid/wiki
Proxool
下載地址:http://proxool.sourceforge.net/
相關(guān)屬性說明:https://github.com/proxool/proxool
2. 數(shù)據(jù)庫連接池
2.1 原理
連接池基本的思想是在系統(tǒng)初始化的時(shí)候,將數(shù)據(jù)庫連接作為對象存儲在內(nèi)存中,當(dāng)用戶需要訪問數(shù)據(jù)庫時(shí),并非建立一個(gè)新的連接,而是從連接池中取出一個(gè)已建立的空閑連接對象。使用完畢后,用戶也并非將連接關(guān)閉,而是將連接放回連接池中,以供下一個(gè)請求訪問使用。而連接的建立、斷開都由連接池自身來管理。同時(shí),還可以通過設(shè)置連接池的參數(shù)來控制連接池中的初始連接數(shù)、連接的上下限數(shù)以及每個(gè)連接的最大使用次數(shù)、最大空閑時(shí)間等等,也可以通過其自身的管理機(jī)制來監(jiān)視數(shù)據(jù)庫連接的數(shù)量、使用情況等。
2.2 常見數(shù)據(jù)庫連接池及其特點(diǎn)
在Java中開源的常用的數(shù)據(jù)庫連接池有以下幾種 :
1)DBCP
DBCP是一個(gè)依賴Jakarta commons-pool對象池機(jī)制的數(shù)據(jù)庫連接池.DBCP可以直接的在應(yīng)用程序中使用,Tomcat的數(shù)據(jù)源使用的就是DBCP。
2)c3p0
c3p0是一個(gè)開放源代碼的JDBC連接池,它在lib目錄中與Hibernate一起發(fā)布,包括了實(shí)現(xiàn)jdbc3和jdbc2擴(kuò)展規(guī)范說明的Connection 和Statement 池的DataSources 對象。
3)Druid
阿里出品,淘寶和支付寶專用數(shù)據(jù)庫連接池,但它不僅僅是一個(gè)數(shù)據(jù)庫連接池,它還包含一個(gè)ProxyDriver,一系列內(nèi)置的JDBC組件庫,一個(gè)SQL Parser。支持所有JDBC兼容的數(shù)據(jù)庫,包括Oracle、MySql、Derby、Postgresql、SQL Server、H2等等。
Druid針對Oracle和MySql做了特別優(yōu)化,比如Oracle的PS Cache內(nèi)存占用優(yōu)化,MySql的ping檢測優(yōu)化。
Druid提供了MySql、Oracle、Postgresql、SQL-92的SQL的完整支持,這是一個(gè)手寫的高性能SQL Parser,支持Visitor模式,使得分析SQL的抽象語法樹很方便。
簡單SQL語句用時(shí)10微秒以內(nèi),復(fù)雜SQL用時(shí)30微秒。
通過Druid提供的SQL Parser可以在JDBC層攔截SQL做相應(yīng)處理,比如說分庫分表、審計(jì)等。Druid防御SQL注入攻擊的WallFilter就是通過Druid的SQL Parser分析語義實(shí)現(xiàn)的。
4) proxool
Proxool是一種Java數(shù)據(jù)庫連接池技術(shù)。sourceforge下的一個(gè)開源項(xiàng)目,這個(gè)項(xiàng)目提供一個(gè)健壯、易用的連接池,最為關(guān)鍵的是這個(gè)連接池提供監(jiān)控的功能,方便易用,便于發(fā)現(xiàn)連接泄漏的情況。目前是和DBCP以及C3P0一起,最為常見的三種JDBC連接池技術(shù)。目前,Hibernate官方宣布由于Bug太多不再支持DBCP,而推薦使用 Proxool或C3P0。
3. 主要配置說明
連接池配置大體可以分為基本配置、關(guān)鍵配置、性能配置等主要配置,缺少proxool相關(guān)配置。
3.1 基本配置
基本配置是指連接池進(jìn)行數(shù)據(jù)庫連接的四個(gè)基本必需配置:
傳遞給JDBC驅(qū)動的用于連接數(shù)據(jù)庫的用戶名、密碼、URL以及驅(qū)動類名。
|
DBCP
|
c3p0
|
Druid
|
用戶名
|
username
|
user
|
username
|
密碼
|
password
|
password
|
password
|
URL
|
url
|
jdbcUrl
|
jdbcUrl
|
驅(qū)動類名
|
driverClassName
|
driverClass
|
driverClassName
|
注:在Druid連接池的配置中,driverClassName可配可不配,如果不配置會根據(jù)url自動識別dbType(數(shù)據(jù)庫類型),然后選擇相應(yīng)的driverClassName。
3.2 關(guān)鍵配置
為了發(fā)揮數(shù)據(jù)庫連接池的作用,在初始化時(shí)將創(chuàng)建一定數(shù)量的數(shù)據(jù)庫連接放到連接池中,這些數(shù)據(jù)庫連接的數(shù)量是由最小數(shù)據(jù)庫連接數(shù)來設(shè)定的。無論這些數(shù)據(jù)庫連接是否被使用,連接池都將一直保證至少擁有這么多的連接數(shù)量。連接池的最大數(shù)據(jù)庫連接數(shù)量限定了這個(gè)連接池能占有的最大連接數(shù),當(dāng)應(yīng)用程序向連接池請求的連接數(shù)超過最大連接數(shù)量時(shí),這些請求將被加入到等待隊(duì)列中。
最小連接數(shù):
是數(shù)據(jù)庫一直保持的數(shù)據(jù)庫連接數(shù),所以如果應(yīng)用程序?qū)?shù)據(jù)庫連接的使用量不大,將有大量的數(shù)據(jù)庫資源被浪費(fèi)。
初始化連接數(shù):
連接池啟動時(shí)創(chuàng)建的初始化數(shù)據(jù)庫連接數(shù)量。
最大連接數(shù):
是連接池能申請的最大連接數(shù),如果數(shù)據(jù)庫連接請求超過此數(shù),后面的數(shù)據(jù)庫連接請求被加入到等待隊(duì)列中。
最大等待時(shí)間:
當(dāng)沒有可用連接時(shí),連接池等待連接被歸還的最大時(shí)間,超過時(shí)間則拋出異常,可設(shè)置參數(shù)為0或者負(fù)數(shù)使得無限等待(根據(jù)不同連接池配置)。
|
DBCP
|
c3p0
|
Druid
|
最小連接數(shù)
|
minIdle(0)
|
minPoolSize(3)
|
minIdle(0)
|
初始化連接數(shù)
|
initialSize(0)
|
initialPoolSize(3)
|
initialSize(0)
|
最大連接數(shù)
|
maxTotal(8)
|
maxPoolSize(15)
|
maxActive(8)
|
最大等待時(shí)間
|
maxWaitMillis(毫秒)
|
maxIdleTime(0秒)
|
maxWait(毫秒)
|
注1:在DBCP連接池的配置中,還有一個(gè)maxIdle的屬性,表示最大空閑連接數(shù),超過的空閑連接將被釋放,默認(rèn)值為8。對應(yīng)的該屬性在Druid連接池已不再使用,配置了也沒有效果,c3p0連接池則沒有對應(yīng)的屬性。
注2:數(shù)據(jù)庫連接池在初始化的時(shí)候會創(chuàng)建initialSize個(gè)連接,當(dāng)有數(shù)據(jù)庫操作時(shí),會從池中取出一個(gè)連接。如果當(dāng)前池中正在使用的連接數(shù)等于maxActive,則會等待一段時(shí)間,等待其他操作釋放掉某一個(gè)連接,如果這個(gè)等待時(shí)間超過了maxWait,則會報(bào)錯(cuò);如果當(dāng)前正在使用的連接數(shù)沒有達(dá)到maxActive,則判斷當(dāng)前是否空閑連接,如果有則直接使用空閑連接,如果沒有則新建立一個(gè)連接。在連接使用完畢后,不是將其物理連接關(guān)閉,而是將其放入池中等待其他操作復(fù)用。
3.3 性能配置
預(yù)緩存設(shè)置:
即是PSCache,PSCache對支持游標(biāo)的數(shù)據(jù)庫性能提升巨大,比如說oracle。JDBC的標(biāo)準(zhǔn)參數(shù),用以控制數(shù)據(jù)源內(nèi)加載的PreparedStatements數(shù)量。但由于預(yù)緩存的statements屬于單個(gè)connection而不是整個(gè)連接池,所以設(shè)置這個(gè)參數(shù)需要考慮到多方面的因素。
單個(gè)連接擁有的最大緩存數(shù):要啟用PSCache,必須配置大于0,當(dāng)大于0時(shí),poolPreparedStatements自動觸發(fā)修改為true。在Druid中,不會存在Oracle下PSCache占用內(nèi)存過多的問題,可以把這個(gè)數(shù)值配置大一些,比如說100
|
DBCP
|
c3p0
|
Druid
|
開啟緩存功能
|
poolPreparedStatements
|
maxStatements
|
poolPreparedStatements
|
單個(gè)連接擁有的最大緩存數(shù)
|
maxOpenPrepared-
Statements
|
maxStatementsPer-
Connection
|
maxOpenPrepared-
Statements
|
連接有效性檢測設(shè)置:
連接池內(nèi)部有機(jī)制判斷,如果當(dāng)前的總的連接數(shù)少于miniIdle,則會建立新的空閑連接,以保證連接數(shù)得到miniIdle。如果當(dāng)前連接池中某個(gè)連接在空閑了timeBetweenEvictionRunsMillis時(shí)間后任然沒有使用,則被物理性的關(guān)閉掉。有些數(shù)據(jù)庫連接的時(shí)候有超時(shí)限制(mysql連接在8小時(shí)后斷開),或者由于網(wǎng)絡(luò)中斷等原因,連接池的連接會出現(xiàn)失效的情況,這時(shí)候設(shè)置一個(gè)testWhileIdle參數(shù)為true,可以保證連接池內(nèi)部定時(shí)檢測連接的可用性,不可用的連接會被拋棄或者重建,最大情況的保證從連接池中得到的Connection對象是可用的。當(dāng)然,為了保證絕對的可用性,你也可以使用testOnBorrow為true(即在獲取Connection對象時(shí)檢測其可用性),不過這樣會影響性能。
|
DBCP
|
c3p0
|
Druid
|
申請連接檢測
|
testOnBorrow
|
testConnectionOnCheckin
|
testOnBorrow
|
是否超時(shí)檢測
|
testWhileIdle
|
|
testWhileIdle
|
空閑時(shí)間
|
timeBetweenEvictionRunsMillis
|
idleConnectionTestPeriod
|
timeBetweenEvictionRunsMillis
|
校驗(yàn)用sql語句
|
validationQuery
|
preferredTestQuery
|
validationQuery
|
歸還連接檢測
|
testOnReturn
|
testConnectionOnCheckout
|
testOnReturn
|
超時(shí)連接關(guān)閉設(shè)置:
removeAbandoned參數(shù),用來檢測到當(dāng)前使用的連接是否發(fā)生了連接泄露,所以在代碼內(nèi)部就假定如果一個(gè)連接建立連接的時(shí)間很長,則將其認(rèn)定為泄露,繼而強(qiáng)制將其關(guān)閉掉。
|
DBCP
|
c3p0
|
Druid
|
是否超時(shí)關(guān)閉連接
|
removeAbandoned
|
breakAfterAcquireFailure
|
removeAbandoned
|
超時(shí)時(shí)間
|
removeAbandonedTimeout
|
checkoutTimeout
|
removeAbandonedTimeout
|
是否記錄日志
|
logAbandoned
|
|
logAbandoned
|
c3p0重連設(shè)置:
設(shè)置獲取連接失敗后,是否重新連接以及間隔時(shí)間。
|
DBCP
|
c3p0
|
Druid
|
重連次數(shù)
|
|
acquireRetryAttempts
|
|
間隔時(shí)間
|
|
acquireRetryDelay
|
|
4. 配置詳解
4.1 DBCP 屬性說明表
屬性(Parameter)
|
默認(rèn)值(Default)
|
描述(Description)
|
username
|
|
傳遞給JDBC驅(qū)動的用于建立連接的用戶名(The connection username to be passed to our JDBC driver to establish a connection.)
|
password
|
|
傳遞給JDBC驅(qū)動的用于建立連接的密碼(The connection password to be passed to our JDBC driver to establish a connection.)
|
url
|
|
傳遞給JDBC驅(qū)動的用于建立連接的URL(The connection URL to be passed to our JDBC driver to establish a connection.)
|
driverClassName
|
|
使用的JDBC驅(qū)動的完整有效的java 類名(The fully qualified Java class name of the JDBC driver to be used.)
|
defaultAutoCommit
|
driver default
|
連接池創(chuàng)建的連接的默認(rèn)的auto-commit狀態(tài),沒有設(shè)置則不會自動提交(The default auto-commit state of connections created by this pool. If not set then the setAutoCommit method will not be called.)
|
initialSize
|
0
|
初始化連接:連接池啟動時(shí)創(chuàng)建的初始化連接數(shù)量(The initial number of connections that are created when the pool is started.
|
maxTotal
|
8
|
最大活動連接:連接池在同一時(shí)間能夠分配的最大活動連接的數(shù)量, 如果設(shè)置為非正數(shù)則表示不限制(The maximum number of active connections that can be allocated from this pool at the same time, or negative for no limit.)
|
maxIdle
|
8
|
最大空閑連接:連接池中容許保持空閑狀態(tài)的最大連接數(shù)量,超過的空閑連接將被釋放,如果設(shè)置為負(fù)數(shù)表示不限制(The maximum number of connections that can remain idle in the pool, without extra ones being released, or negative for no limit.)
|
minIdle
|
0
|
最小空閑連接:連接池中容許保持空閑狀態(tài)的最小連接數(shù)量,負(fù)數(shù)表示沒有現(xiàn)在(The maximum number of connections that can remain idle in the pool, without extra ones being released, or negative for no limit.)
|
注意:如果在某些負(fù)載比較大的系統(tǒng)中將maxIdel設(shè)置過小時(shí),很可能會出現(xiàn)連接關(guān)閉的同時(shí)新連接馬上打開的情況.這是由于關(guān)閉連接的線程比打開的快導(dǎo)致的.所以,對于這種系統(tǒng)中,maxIdle的設(shè)定值是不同的但是通常首選默認(rèn)值
|
||
(NOTE: If maxIdle is set too low on heavily loaded systems it is possible you will see connections being closed and almost immediately new connections being opened. This is a result of the active threads momentarily closing connections faster than they are opening them, causing the number of idle connections to rise above maxIdle. The best value for maxIdle for heavily loaded system will vary but the default is a good starting point.)
|
||
maxWaitMillis
|
indefinitely
|
最大等待時(shí)間:當(dāng)沒有可用連接時(shí),連接池等待連接被歸還的最大時(shí)間(以毫秒計(jì)數(shù)),超過時(shí)間則拋出異常,如果設(shè)置為-1表示無限等待(The maximum number of milliseconds that the pool will wait (when there are no available connections) for a connection to be returned before throwing an exception, or -1 to wait indefinitely.)
|
validationQuery
|
|
SQL查詢,用來驗(yàn)證從連接池取出的連接,在將連接返回給調(diào)用者之前.如果指定,則查詢必須是一個(gè)SQL SELECT并且必須返回至少一行記錄(The SQL query that will be used to validate connections from this pool before returning them to the caller. If specified, this query MUST be an SQL SELECT statement that returns at least one row. If not specified, connections will be validation by calling the isValid() method.)
|
testOnCreate
|
false
|
指明是否在建立連接之后進(jìn)行驗(yàn)證,如果驗(yàn)證失敗,則嘗試重新建立連接(The indication of whether objects will be validated after creation. If the object fails to validate, the borrow attempt that triggered the object creation will fail.)
|
testOnBorrow
|
true
|
指明是否在從池中取出連接前進(jìn)行檢驗(yàn),如果檢驗(yàn)失敗,則從池中去除連接并嘗試取出另一個(gè). 注意:設(shè)置為true后如果要生效,validationQuery參數(shù)必須設(shè)置為非空字符串(The indication of whether objects will be validated before being borrowed from the pool. If the object fails to validate, it will be dropped from the pool, and we will attempt to borrow another.)
|
testOnReturn
|
false
|
指明是否在歸還到池中前進(jìn)行檢驗(yàn)(The indication of whether objects will be validated before being returned to the pool.)
|
testWhileIdle
|
false
|
指明連接是否被空閑連接回收器(如果有)進(jìn)行檢驗(yàn).如果檢測失敗,則連接將被從池中去除. 注意:設(shè)置為true后如果要生效,validationQuery參數(shù)必須設(shè)置為非空字符串(The indication of whether objects will be validated by the idle object evictor (if any). If an object fails to validate, it will be dropped from the pool.)
|
timeBetweenEviction-
RunsMillis
|
-1
|
在空閑連接回收器線程運(yùn)行期間休眠的時(shí)間值,以毫秒為單位.如果設(shè)置為非正數(shù),則不運(yùn)行空閑連接回收器線程(The number of milliseconds to sleep between runs of the idle object evictor thread. When non-positive, no idle object evictor thread will be run.)
|
numTestsPerEvictionRun
|
3
|
在每次空閑連接回收器線程(如果有)運(yùn)行時(shí)檢查的連接數(shù)量(The number of objects to examine during each run of the idle object evictor thread (if any).)
|
minEvictableIdleTime-Millis
|
1000*60*30
|
連接在池中保持空閑而不被空閑連接回收器線程(如果有)回收的最小時(shí)間值,單位毫秒(The minimum amount of time an object may sit idle in the pool before it is eligable for eviction by the idle object evictor (if any).)
|
softMiniEvictableIdle- TimeMillis
|
-1
|
說明(The minimum amount of time a connection may sit idle in the pool before it is eligible for eviction by the idle connection evictor, with the extra condition that at least "minIdle" connections remain in the pool. When miniEvictableIdleTimeMillis is set to a positive value, miniEvictableIdleTimeMillis is examined first by the idle connection evictor - i.e. when idle connections are visited by the evictor, idle time is first compared against miniEvictableIdleTimeMillis (without considering the number of idle connections in the pool) and then against softMinEvictableIdleTimeMillis, including the minIdle constraint.)
|
maxConnLifetimeMillis
|
-1
|
說明(The maximum lifetime in milliseconds of a connection. After this time is exceeded the connection will fail the next activation, passivation or validation test. A value of zero or less means the connection has an infinite lifetime.)
|
logExpiredConnections
|
true
|
說明(Flag to log a message indicating that a connection is being closed by the pool due to maxConnLifetimeMillis exceeded. Set this property to false to suppress expired connection logging that is turned on by default.
|
connectionInitSqls
|
null
|
說明(A Collection of SQL statements that will be used to initialize physical connections when they are first created. These statements are executed only once - when the configured connection factory creates the connection.)
|
info
|
true
|
說明(True means that borrowObject returns the most recently used ("last in") connection in the pool (if there are idle connections available). False means that the pool behaves as a FIFO queue - connections are taken from the idle instance pool in the order that they are returned to the pool.)
|
poolPreparedState-ments
|
false
|
開啟池的prepared statement 池功能(Enable prepared statement pooling for this pool.)
|
maxOpenPreparedState-ments
|
unlimited
|
statement池能夠同時(shí)分配的打開的statements的最大數(shù)量, 如果設(shè)置為0表示不限制(The maximum number of open statements that can be allocated from the statement pool at the same time, or negative for no limit.)
|
NOTE - Make sure your connection has some resources left for the other statements. Pooling PreparedStatements may keep their cursors open in the database, causing a connection to run out of cursors, especially if maxOpenPreparedStatements is left at the default (unlimited) and an application opens a large number of different PreparedStatements per connection. To avoid this problem, maxOpenPreparedStatements should be set to a value less than the maximum number of cursors that can be open on a Connection.
|
||
accessToUnderlyingConnectionAllowed
|
false
|
控制PoolGuard是否容許獲取底層連接(Controls if the PoolGuard allows access to the underlying connection.) 默認(rèn)false不開啟,這是一個(gè)有潛在危險(xiǎn)的功能, 不適當(dāng)?shù)木幋a會造成傷害.(關(guān)閉底層連接或者在守護(hù)連接已經(jīng)關(guān)閉的情況下繼續(xù)使用它).請謹(jǐn)慎使用,并且僅當(dāng)需要直接訪問驅(qū)動的特定功能時(shí)使用.注意: 不要關(guān)閉底層連接, 只能關(guān)閉前面的那個(gè). Default is false, it is a potential dangerous operation and misbehaving programs can do harmful things. (closing the underlying or continue using it when the guarded connection is already closed) Be careful and only use when you need direct access to driver specific extensions. NOTE: Do not close the underlying connection, only the original one.
|
removeAbandoned
|
false
|
標(biāo)記是否刪除泄露的連接,如果他們超過了removeAbandonedTimout的限制.如果設(shè)置為true, 連接被認(rèn)為是被泄露并且可以被刪除,如果空閑時(shí)間超過removeAbandonedTimeout. 設(shè)置為true可以為寫法糟糕的沒有關(guān)閉連接的程序修復(fù)數(shù)據(jù)庫連接. (Flags to remove abandoned connections if they exceed the removeAbandonedTimout. A connection is considered abandoned and eligible for removal if it has not been used for longer than removeAbandonedTimeout. Setting one or both of these to true can recover db connections from poorly written applications which fail to close connections.)
|
removeAbandonedTimeout
|
300
|
泄露的連接可以被刪除的超時(shí)值, 單位秒(Timeout in seconds before an abandoned connection can be removed.)
|
logAbandoned
|
false
|
標(biāo)記當(dāng)Statement或連接被泄露時(shí)是否打印程序的stack traces日志。被泄露的Statements和連接的日志添加在每個(gè)連接打開或者生成新的Statement,因?yàn)樾枰蓅tack trace。(Flag to log stack traces for application code which abandoned a Statement or Connection. Logging of abandoned Statements and Connections adds overhead for every Connection open or new Statement because a stack trace has to be generated.)
|
abandonedUsageTracking
|
false
|
如果為true, 那么連接池會記錄每個(gè)方法調(diào)用時(shí)候的堆棧信息以及廢棄連接的調(diào)試信息(If true, the connection pool records a stack trace every time a method is called on a pooled connection and retains the most recent stack trace to aid debugging of abandoned connections. There is significant overhead added by setting this to true.)
|
注:如果開啟"removeAbandoned",那么連接在被認(rèn)為泄露時(shí)可能被池回收. 這個(gè)機(jī)制在(getNumIdle() < 2)and (getNumActive() > getMaxActive() - 3)時(shí)被觸發(fā). 舉例當(dāng)maxActive=20, 活動連接為18,空閑連接為1時(shí)可以觸發(fā)"removeAbandoned".但是活動連接只有在沒有被使用的時(shí)間超過"removeAbandonedTimeout"時(shí)才被刪除,默認(rèn)300秒.在resultset中游歷不被計(jì)算為被使用.
|
If you have enabled removeAbandonedOnMaintenance or removeAbandonedOnBorrow then it is possible that a connection is reclaimed by the pool because it is considered to be abandoned. This mechanism is triggered when (getNumIdle() < 2) and (getNumActive() > getMaxTotal() - 3) and removeAbandonedOnBorrow is true; or after eviction finishes and removeAbandonedOnMaintenance is true. For example, maxTotal=20 and 18 active connections and 1 idle connection would trigger removeAbandonedOnBorrow, but only the active connections that aren't used for more then "removeAbandonedTimeout" seconds are removed (default 300 sec). Traversing a resultset doesn't count as being used. Creating a Statement, PreparedStatement or CallableStatement or using one of these to execute a query (using one of the execute methods) resets the lastUsed property of the parent connection.
|
4.2 C3P0 屬性說明表
屬性(Parameter)
|
默認(rèn)值(Default)
|
描述(Description)
|
user
|
|
同DBCP中的username屬性
|
password
|
|
同DBCP中的password屬性
|
jdbcUrl
|
|
同DBCP中的jdbcUrl屬性
|
driverClass
|
|
同DBCP中的driverClass屬性
|
autoCommitOnClose
|
false
|
默認(rèn)值false表示回滾任何未提交的任務(wù),設(shè)置為true則全部提交,而不是在關(guān)閉連接之前回滾
(C3P0's default policy is to rollback any uncommitted, pending work. Setting autoCommitOnClose to true causes uncommitted pending work to be committed, rather than rolled back on Connection close.)
*參見DBCP中的defaultAutoCommit屬性
|
initialPoolSize
|
3
|
初始化連接:連接池啟動時(shí)創(chuàng)建的初始化連接數(shù)量(The initial number of connections that are created when the pool is started.
*參見DBCP中的initialSize屬性
|
maxPoolSize
|
15
|
連接池中保留的最大連接數(shù)(Maximum number of Connections a pool will maintain at any given time.) *參見DBCP中的maxIdle屬性
|
minPoolSize
|
3
|
連接池中保留的最小連接數(shù)(Minimum number of Connections a pool will maintain at any given time.) *參見DBCP中的maxIdle屬性
|
maxIdleTime
|
0
|
最大等待時(shí)間:當(dāng)沒有可用連接時(shí),連接池等待連接被歸還的最大時(shí)間(以秒計(jì)數(shù)),超過時(shí)間則拋出異常,如果設(shè)置為0表示無限等待(Seconds a Connection can remain pooled but unused before being discarded. Zero means idle connections never expire.) *參見DBCP中maxWaitMillis 屬性
|
preferredTestQuery
|
null
|
定義所有連接測試都執(zhí)行的測試語句。在使用連接測試的情況下這個(gè)一顯著提高測試速度。注意:測試的表必須在初始數(shù)據(jù)源的時(shí)候就存在。(Defines the query that will be executed for all connection tests, if the default ConnectionTester (or some other implementation of QueryConnectionTester, or better yet FullQueryConnectionTester) is being used. Defining a preferredTestQuery that will execute quickly in your database may dramatically speed up Connection tests.)
|
testConnectionOn- Checkin
|
false
|
如果設(shè)為true那么在取得連接的同時(shí)將校驗(yàn)連接的有效性。(If true, an operation will be performed asynchronously at every connection checkin to verify that the connection is valid. Use in combination with idleConnectionTestPeriod for quite reliable, always asynchronous Connection testing.) *參見DBCP中的testOnBorrow屬性
|
testConnectionOn- Checkout
|
false
|
如果設(shè)為true那么在每個(gè)connection提交的時(shí)候都將校驗(yàn)其有效性,但是要確保配置的preferredTestQuery的有效性(If true, an operation will be performed at every connection checkout to verify that the connection is valid. Be sure to set an efficient preferredTestQuery or automaticTestTable if you set this to true.) *參見DBCP中的testOnBorrow屬性
|
idleConnectionTest- Period
|
0
|
如果設(shè)置大于0,表示過了多少秒檢查一次空閑連接,結(jié)合testConnectionOnCheckin以及testConnectionOnCheckout使用(If this is a number greater than 0, c3p0 will test all idle, pooled but unchecked-out connections, every this number of seconds.)
|
acquireRetryAttempts
|
30
|
定義在從數(shù)據(jù)庫獲取新連接失敗后重復(fù)嘗試的次數(shù), 如果小于0則表示無限制的連接。(Defines how many times c3p0 will try to acquire a new Connection from the database before giving up. If this value is less than or equal to zero, c3p0 will keep trying to fetch a Connection indefinitely.)
|
acquireRetryDelay
|
1000
|
兩次連接中的間隔時(shí)間,單位毫秒。(Milliseconds, time c3p0 will wait between acquire attempts.)
|
breakAfterAcquire-
Failure
|
false
|
獲取連接失敗將會引起所有等待連接池來獲取連接的線程拋出異常。但是數(shù)據(jù)源仍有效保留,并在下次調(diào)用 getConnection() 的時(shí)候繼續(xù)嘗試獲取連接。如果為 true,那么在嘗試獲取連接失敗后該數(shù)據(jù)源將聲明已斷開并永久關(guān)閉。(If true, a pooled DataSource will declare itself broken and be permanently closed if a Connection cannot be obtained from the database after making acquireRetryAttempts to acquire one. If false, failure to obtain a Connection will cause all Threads waiting for the pool to acquire a Connection to throw an Exception, but the DataSource will remain valid, and will attempt to acquire again following a call to getConnection().)
|
checkoutTimeout
|
0
|
當(dāng)連接池用完時(shí)客戶端調(diào)用 getConnection()后等待獲取新連接的時(shí)間,潮濕后將拋出SQLException,如設(shè)為0,則為無限期等待。單位毫秒。(The number of milliseconds a client calling getConnection() will wait for a Connection to be checked-in or acquired when the pool is exhausted. Zero means wait indefinitely. Setting any positive value will cause the getConnection() call to time-out and break with an SQLException after the specified number of milliseconds.)
|
maxStatements
|
0
|
控制數(shù)據(jù)源內(nèi)加載的PreparedStatements數(shù)量(Enable prepared statement pooling for this pool.)
|
maxStatementsPer- Connection
|
0
|
定義了連接池內(nèi)單個(gè)連接所擁有的最大緩存statements數(shù)(The maximum number of open statements that can be allocated from the statement pool at the same time, or negative for no limit.)
|
4.3 DRUID 屬性說明表
屬性(Parameter)
|
默認(rèn)值(Default)
|
描述(Description)
|
username
|
|
連接數(shù)據(jù)庫的用戶名
|
password
|
|
連接數(shù)據(jù)庫的密碼
|
jdbcUrl
|
|
同DBCP中的jdbcUrl屬性
|
driverClassName
|
根據(jù)url自動識別
|
這一項(xiàng)可配可不配,如果不配置druid會根據(jù)url自動識別dbType,然后選擇相應(yīng)的driverClassName
|
initialSize
|
0
|
初始化時(shí)建立物理連接的個(gè)數(shù)。初始化發(fā)生在顯示調(diào)用init方法,或者第一次getConnection時(shí) *參見DBCP中的initialSize屬性
|
maxActive
|
8
|
最大連接池?cái)?shù)量(Maximum number of Connections a pool will maintain at any given time.) *參見DBCP中的maxTotal屬性
|
maxIdle
|
8
|
已經(jīng)不再使用,配置了也沒效果 *參見DBCP中的maxIdle屬性
|
minIdle
|
|
最小連接池?cái)?shù)量
|
maxWait
|
|
獲取連接時(shí)最大等待時(shí)間,單位毫秒。配置了maxWait之后,缺省啟用公平鎖,并發(fā)效率會有所下降,如果需要可以通過配置useUnfairLock屬性為true使用非公平鎖。
|
poolPreparedState- ments
|
false
|
是否緩存preparedStatement,也就是PSCache。PSCache對支持游標(biāo)的數(shù)據(jù)庫性能提升巨大,比如說oracle。
|
maxOpenPrepared- Statements
|
-1
|
要啟用PSCache,必須配置大于0,當(dāng)大于0時(shí),poolPreparedStatements自動觸發(fā)修改為true。 在Druid中,不會存在Oracle下PSCache占用內(nèi)存過多的問題,可以把這個(gè)數(shù)值配置大一些,比如說100
|
testOnBorrow
|
true
|
申請連接時(shí)執(zhí)行validationQuery檢測連接是否有效,做了這個(gè)配置會降低性能。
|
testOnReturn
|
false
|
歸還連接時(shí)執(zhí)行validationQuery檢測連接是否有效,做了這個(gè)配置會降低性能
|
testWhileIdle
|
false
|
建議配置為true,不影響性能,并且保證安全性。申請連接的時(shí)候檢測,如果空閑時(shí)間大于timeBetweenEvictionRunsMillis,執(zhí)行validationQuery檢測連接是否有效。
|
validationQuery
|
|
用來檢測連接是否有效的sql,要求是一個(gè)查詢語句。如果validationQuery為null,testOnBorrow、testOnReturn、testWhileIdle都不會其作用。在mysql中通常為select 'x',在oracle中通常為select 1 from dual
|
timeBetweenEviction-RunsMillis
|
|
1) Destroy線程會檢測連接的間隔時(shí)間 2) testWhileIdle的判斷依據(jù)
|
minEvictableIdle- TimeMillis
|
|
Destory線程中如果檢測到當(dāng)前連接的最后活躍時(shí)間和當(dāng)前時(shí)間的差值大于minEvictableIdleTimeMillis,則關(guān)閉當(dāng)前連接。
|
removeAbandoned
|
|
對于建立時(shí)間超過removeAbandonedTimeout的連接強(qiáng)制關(guān)閉
|
removeAbandoned-Timeout
|
|
指定連接建立多長時(shí)間就需要被強(qiáng)制關(guān)閉
|
logAbandoned
|
false
|
指定發(fā)生removeabandoned的時(shí)候,是否記錄當(dāng)前線程的堆棧信息到日志中
|
filters
|
|
屬性類型是字符串,通過別名的方式配置擴(kuò)展插件,常用的插件有: 1)監(jiān)控統(tǒng)計(jì)用的filter:stat 2)日志用的filter:log4j 3)防御sql注入的filter:wall
|