1. 程式人生 > >三大資料庫連線池分析

三大資料庫連線池分析

先來點實用的:

<!-- 配置dbcp資料來源 -->

<bean id="dataSource2" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">

<property name="driverClassName" value="${jdbc.driverClassName}"/>

<property name="url" value="${jdbc.url}"/>

<property name="username" value="${jdbc.username}"/>

<property name="password" value="${jdbc.password}"/>

<!-- 池啟動時建立的連線數量 -->

<property name="initialSize" value="5"/>

<!-- 同一時間可以從池分配的最多連線數量。設定為0時表示無限制。 -->

<property name="maxActive" value="30"/>

<!-- 池裡不會被釋放的最多空閒連線數量。設定為0時表示無限制。 -->

<property name="maxIdle" value="20"/>

<!-- 在不新建連線的條件下,池中保持空閒的最少連線數。 -->

<property name="minIdle" value="3"/>

<!-- 設定自動回收超時連線 -->

<property name="removeAbandoned" value="true" />

<!-- 自動回收超時時間(以秒數為單位) -->

<property name="removeAbandonedTimeout" value="200"/>

<!-- 設定在自動回收超時連線的時候列印連線的超時錯誤 -->

<property name="logAbandoned" value="true"/>

<!-- 等待超時以毫秒為單位,在丟擲異常之前,池等待連線被回收的最長時間(當沒有可用連線時)。設定為-1表示無限等待。 -->

<property name="maxWait" value="100"/>

</bean>

<!-- 配置c3p0資料來源 -->

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">

<property name="jdbcUrl" value="${jdbc.url}" />

<property name="driverClass" value="${jdbc.driverClassName}" />

<property name="user" value="${jdbc.username}" />

<property name="password" value="${jdbc.password}" />

<!--連線池中保留的最大連線數。Default: 15 -->

<property name="maxPoolSize" value="100" />

<!--連線池中保留的最小連線數。-->

<property name="minPoolSize" value="1" />

<!--初始化時獲取的連線數,取值應在minPoolSize與maxPoolSize之間。Default: 3 -->

<property name="initialPoolSize" value="10" />

<!--最大空閒時間,60秒內未使用則連線被丟棄。若為0則永不丟棄。Default: 0 -->

<property name="maxIdleTime" value="30" />

<!--當連線池中的連線耗盡的時候c3p0一次同時獲取的連線數。Default: 3 -->

<property name="acquireIncrement" value="5" />

<!--JDBC的標準引數,用以控制資料來源內載入的PreparedStatements數量。但由於預快取的statements

屬於單個connection而不是整個連線池。所以設定這個引數需要考慮到多方面的因素。

如果maxStatements與maxStatementsPerConnection均為0,則快取被關閉。Default: 0-->

<property name="maxStatements" value="0" />

<!--每60秒檢查所有連線池中的空閒連線。Default: 0 -->

<property name="idleConnectionTestPeriod" value="60" />

<!--定義在從資料庫獲取新連線失敗後重復嘗試的次數。Default: 30 -->

<property name="acquireRetryAttempts" value="30" />

<!--獲取連線失敗將會引起所有等待連線池來獲取連線的執行緒丟擲異常。但是資料來源仍有效

保留,並在下次呼叫getConnection()的時候繼續嘗試獲取連線。如果設為true,那麼在嘗試

獲取連線失敗後該資料來源將申明已斷開並永久關閉。Default: false-->

<property name="breakAfterAcquireFailure" value="true" />

<!--因效能消耗大請只在需要的時候使用它。如果設為true那麼在每個connection提交的

時候都將校驗其有效性。建議使用idleConnectionTestPeriod或automaticTestTable

等方法來提升連線測試的效能。Default: false -->

<property name="testConnectionOnCheckout" value="false" />

</bean>

<!-- 阿里 druid資料庫連線池 -->

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"

init-method="init" destroy-method="close">

<!-- 基本屬性 url、user、password -->

<property name="url" value="${db.mysql.url}" />

<property name="username" value="${db.mysql.username}" />

<property name="password" value="${db.mysql.password}" />

<property name="driverClassName" value="${driverClassName}" />

<!-- 配置初始化大小、最小、最大 -->

<property name="initialSize" value="5" />

<property name="minIdle" value="10" />

<property name="maxActive" value="20" />

<!-- 配置獲取連線等待超時的時間 -->

<property name="maxWait" value="60000" />

<!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閒連線,單位是毫秒 -->

<property name="timeBetweenEvictionRunsMillis" value="60000" />

<!-- 配置一個連線在池中最小生存的時間,單位是毫秒 -->

<property name="minEvictableIdleTimeMillis" value="300000" />

<property name="validationQuery" value="SELECT 'x'" />

<property name="testWhileIdle" value="true" />

<property name="testOnBorrow" value="false" />

<property name="testOnReturn" value="false" />

<!-- 開啟PSCache,並且指定每個連線上PSCache的大小 -->

<property name="poolPreparedStatements" value="true" />

<property name="maxPoolPreparedStatementPerConnectionSize" value="20" />

<!-- 連線洩漏處理。Druid提供了RemoveAbandanded相關配置,用來關閉長時間不使用的連線(例如忘記關閉連線)。 -->

<property name="removeAbandoned" value="true" />

<!-- 1800秒,也就是30分鐘 -->

<property name="removeAbandonedTimeout" value="1800" />

<!-- 關閉abanded連線時輸出錯誤日誌 -->

<property name="logAbandoned" value="true" />

<!-- 配置監控統計攔截的filters, 監控統計:"stat",防SQL注入:"wall",組合使用: "stat,wall" -->

<property name="filters" value="stat" />

</bean>

區別比較

c3p0、dbcp、druid三大連線池的區別
c3p0 c3p0是一個開放原始碼的JDBC連線池,它在lib目錄中與Hibernate一起釋出,包括了實現jdbc3和jdbc2擴充套件規範說明的Connection 和Statement 池的DataSources 物件。

DBCP DBCP是一個依賴Jakarta commons-pool物件池機制的資料庫連線池.DBCP可以直接的在應用程式中使用,Tomcat的資料來源使用的就是DBCP。
druid 阿里出品,淘寶和支付寶專用資料庫連線池,但它不僅僅是一個數據庫連線池,它還包含一個ProxyDriver,一系列內建的JDBC元件庫,一個 SQL Parser。支援所有JDBC相容的資料庫,包括Oracle、MySql、Derby、Postgresql、SQL Server、H2等等。Druid針對Oracle和MySql做了特別優化,比如Oracle的PS Cache記憶體佔用優化,MySql的ping檢測優化。Druid提供了MySql、Oracle、Postgresql、SQL-92的SQL的完整支援,這是一個手寫的高效能SQL Parser,支援Visitor模式,使得分析SQL的抽象語法樹很方便。簡單SQL語句用時10微秒以內,複雜SQL用時30微秒。通過Druid提供的SQL Parser可以在JDBC層攔截SQL做相應處理,比如說分庫分表、審計等。Druid防禦SQL注入攻擊的WallFilter就是通過Druid的SQL Parser分析語義實現的。
屬性對比
連線池的配置大體可以分為:基本配置、關鍵配置、效能配置等主要配置
基本配置:連線池進行資料庫連線的四個必須配置

  DBPC C3P0 Druid

使用者名稱 username uer username
密碼 password password password
URL url jdbcUrl jdbcUrl
驅動類名 driverClassName driverClass driverClassName
注:在Druid連線池配置中,driverClassName可配可不配,不配置的話可以根據url自動識別資料庫型別,然後選擇相應的driverClassName。
關鍵配置:為了發揮資料庫連線池的作用。

  DBCP c3p0 Druid

最小連線數 minldle(0) miniPoolSize(3) minldle(0)
初始化連線數 innitialSize(0) initialPoolSize(3) initialSize
最大連線數 maxTotal(8) maxPoolSize(15) maxActive(8)
最大等待時間 maxWaitMillis(毫秒) maxIdleTime(0秒) maxWait(毫秒)

最小連線數 是資料庫一直保持的資料庫連線數
初始化連線數 連線池啟動時建立的初始化資料庫連線數量
最大連線數 連線池能申請的最大連線數,請求超出此數時,後面的資料庫連線請求被加入等待佇列中。
最大等待時間 當沒有可用連線時,連線池等待連線被歸還的最大時間,超過時間則丟擲異常,可設定為0或負數,無限等待。
在DBCP連線池的配置中,還有一個maxldle的屬性,表示最大空閒連線數,超過的空閒連線將被釋放。對應的該屬性在Druid中不再使用,配置了也不會有效果;而c3p0就沒有對應的屬性。
資料庫連線池在初始化的時候回建立initialSize個連線,當有資料庫操作時,會從池中取出一個連線。如果連線數等於maxActive,則會等待一段時間,等待其他操作釋放掉一個連線,如果這個時間超過了maxWait,就會報錯如果當前使用的數量沒有達到maxActive,則會判斷當前是否空閒連線,有的話,直接使用空閒連線,沒有的話,則新建一個連線。連線使用完畢後,放入池中,等待其他操作複用。
效能配置:預快取設定、連線有效性檢測設定、連線超時關閉設定
預快取設定:用於控制PreparedStatement數量,提升資料庫效能。

  DBCP c3p0 Druid
開啟快取功能 poolPreparedStatements maxStatements poolPreparedStatements
單個連線擁有的最大快取數 maxOpenPreparedStatements maxStatementsPerConnection maxOpenPreparedStatements
連線有效性檢測設定:連線池內部有機制判斷,如果當前的總連線數少於minildle,則會建立新的空閒連線,以保證連線數達到minildle。如果當前連線池中某個連線處於空閒,則被物理性的關閉掉。有些資料庫連線的時候有超時的限制(mysql連線8小時後斷開),或者由於網路中斷等原因,連線池的連線會出現失效,這時候,設定一個testWhileldle引數為true,可以保證連線池中,定時檢測連線可用性,不可用的連線會被丟擲或者重建,保證池中connection可用。

  DBCP c3p0 Druid
申請連線檢測 testOnBorrow testConnectionOnCheckin testOnBorrow
是否超時檢測 testWhileldle   testWhileldle
空閒時間 timeBetweenEvictionRuns Millis idleConnectionTestPeriod timeBetweenEvictionRunsMillis
校驗sql語句 validationQuery preferredTestQuery validationQuery
歸還連線檢測 testOnReturn testConnectionOnCheckout testOnReturn
超時連線關閉設定:用來檢測當前使用的連線是否發生洩漏,所以在程式碼內部就假定如果一個連線建立連線時間很長,則認定為洩漏,繼而強制關閉。

  DBCP c3p0 Druid
是否超時關閉連線 removeAbandoned breakAfterAcquireFailure removeAbandoned
超時時間 removeAbandonedTimeout checkoutTimeout removeAbandonedTimeout
是否記錄日誌 logAbandoned   logAbandoned
配置詳解
DBCP 屬性說明表
屬性(Parameter) 預設值(Default) 描述(Deion)
username   傳遞給JDBC驅動的用於建立連線的使用者名稱(The connection username to be passed to our JDBC driver to establish a connection.)
password   傳遞給JDBC驅動的用於建立連線的密碼(The connection password to be passed to our JDBC driver to establish a connection.)
url   傳遞給JDBC驅動的用於建立連線的URL(The connection URL to be passed to our JDBC driver to establish a connection.)
driverClassName   使用的JDBC驅動的完整有效的java 類名(The fully qualified Java class name of the JDBC driver to be used.)
defaultAutoCommit driver default 連線池建立的連線的預設的auto-commit狀態,沒有設定則不會自動提交(The default auto-commit state of connections created by this pool. If not set then the setAutoCommit method will not be called.)
initialSize 0 初始化連線:連線池啟動時建立的初始化連線數量(The initial number of connections that are created when the pool is started.
maxTotal 8 最大活動連線:連線池在同一時間能夠分配的最大活動連線的數量, 如果設定為非正數則表示不限制(The maximum number of active connections that can be allocated from this pool at the same time, or negative for no limit.)
maxIdle 8 最大空閒連線:連線池中容許保持空閒狀態的最大連線數量,超過的空閒連線將被釋放,如果設定為負數表示不限制(The maximum number of connections that can remain idle in the pool, without extra ones being released, or negative for no limit.)
minIdle 0 最小空閒連線:連線池中容許保持空閒狀態的最小連線數量,負數表示沒有現在(The maximum number of connections that can remain idle in the pool, without extra ones being released, or negative for no limit.)
注意:如果在某些負載比較大的系統中將maxIdel設定過小時,很可能會出現連線關閉的同時新連線馬上開啟的情況.這是由於關閉連線的執行緒比開啟的快導致的.所以,對於這種系統中,maxIdle的設定值是不同的但是通常首選預設值
(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 最大等待時間:當沒有可用連線時,連線池等待連線被歸還的最大時間(以毫秒計數),超過時間則丟擲異常,如果設定為-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查詢,用來驗證從連線池取出的連線,在將連線返回給呼叫者之前.如果指定,則查詢必須是一個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 指明是否在建立連線之後進行驗證,如果驗證失敗,則嘗試重新建立連線(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 指明是否在從池中取出連線前進行檢驗,如果檢驗失敗,則從池中去除連線並嘗試取出另一個. 注意: 設定為true後如果要生效,validationQuery引數必須設定為非空字串(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 指明是否在歸還到池中前進行檢驗(The indication of whether objects will be validated before being returned to the pool.)
testWhileIdle FALSE 指明連線是否被空閒連接回收器(如果有)進行檢驗.如果檢測失敗,則連線將被從池中去除. 注意: 設定為true後如果要生效,validationQuery引數必須設定為非空字串(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 在空閒連接回收器執行緒執行期間休眠的時間值,以毫秒為單位.如果設定為非正數,則不執行空閒連接回收器執行緒(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 在每次空閒連接回收器執行緒(如果有)執行時檢查的連線數量(The number of objects to examine during each run of the idle object evictor thread (if any).)
minEvictableIdleTime-Millis 1000*60*30 連線在池中保持空閒而不被空閒連接回收器執行緒(如果有)回收的最小時間值,單位毫秒(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池能夠同時分配的開啟的statements的最大數量, 如果設定為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.) 預設false不開啟, 這是一個有潛在危險的功能, 不適當的編碼會造成傷害.(關閉底層連線或者在守護連線已經關閉的情況下繼續使用它).請謹慎使用,並且僅當需要直接訪問驅動的特定功能時使用.注意: 不要關閉底層連線, 只能關閉前面的那個. 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 標記是否刪除洩露的連線,如果他們超過了removeAbandonedTimout的限制.如果設定為true, 連線被認為是被洩露並且可以被刪除,如果空閒時間超過removeAbandonedTimeout. 設定為true可以為寫法糟糕的沒有關閉連線的程式修復資料庫連線. (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 洩露的連線可以被刪除的超時值, 單位秒(Timeout in seconds before an abandoned connection can be removed.)
logAbandoned FALSE 標記當Statement或連線被洩露時是否列印程式的stack traces日誌。被洩露的Statements和連線的日誌新增在每個連線開啟或者生成新的Statement,因為需要生成stack 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, 那麼連線池會記錄每個方法呼叫時候的堆疊資訊以及廢棄連線的除錯資訊(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",那麼連線在被認為洩露時可能被池回收. 這個機制在(getNumIdle() < 2)and (getNumActive() > getMaxActive() - 3)時被觸發. 舉例當maxActive=20, 活動連線為18,空閒連線為1時可以觸發"removeAbandoned".但是活動連線只有在沒有被使用的時間超過"removeAbandonedTimeout"時才被刪除,預設300秒.在resultset中游歷不被計算為被使用.
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.
C3P0 屬性說明表
屬性(Parameter) 預設值(Default) 描述(Deion)
user   同DBCP中的username屬性
password   同DBCP中的password屬性
jdbcUrl   同DBCP中的jdbcUrl屬性
driverClass   同DBCP中的driverClass屬性
autoCommitOnClose FALSE 預設值false表示回滾任何未提交的任務,設定為true則全部提交,而不是在關閉連線之前回滾(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 初始化連線:連線池啟動時建立的初始化連線數量(The initial number of connections that are created when the pool is started.*參見DBCP中的initialSize屬性
maxPoolSize 15 連線池中保留的最大連線數(Maximum number of Connections a pool will maintain at any given time.) *參見DBCP中的maxIdle屬性
minPoolSize 3 連線池中保留的最小連線數(Minimum number of Connections a pool will maintain at any given time.) *參見DBCP中的maxIdle屬性
maxIdleTime 0 最大等待時間:當沒有可用連線時,連線池等待連線被歸還的最大時間(以秒計數),超過時間則丟擲異常,如果設定為0表示無限等待(Seconds a Connection can remain pooled but unused before being discarded. Zero means idle connections never expire.) *參見DBCP中maxWaitMillis 屬性
preferredTestQuery null 定義所有連線測試都執行的測試語句。在使用連線測試的情況下這個一顯著提高測試速度。注意:測試的表必須在初始資料來源的時候就存在。(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 如果設為true那麼在取得連線的同時將校驗連線的有效性。(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 如果設為true那麼在每個connection提交的時候都將校驗其有效性,但是要確保配置的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 如果設定大於0,表示過了多少秒檢查一次空閒連線,結合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 定義在從資料庫獲取新連線失敗後重復嘗試的次數, 如果小於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 兩次連線中的間隔時間,單位毫秒。(Milliseconds, time c3p0 will wait between acquire attempts.)
breakAfterAcquire-Failure FALSE 獲取連線失敗將會引起所有等待連線池來獲取連線的執行緒丟擲異常。但是資料來源仍有效保留,並在下次呼叫 getConnection() 的時候繼續嘗試獲取連線。如果為 true,那麼在嘗試獲取連線失敗後該資料來源將宣告已斷開並永久關閉。(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 當連線池用完時客戶端呼叫getConnection() 後等待獲取新連線的時間,潮溼後將丟擲SQLException,如設為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 控制資料來源內載入的PreparedStatements數量(Enable prepared statement pooling for this pool.)
maxStatementsPer- Connection 0 定義了連線池內單個連線所擁有的最大快取statements數(The maximum number of open statements that can be allocated from the statement pool at the same time, or negative for no limit.)
DRUID 屬性說明表
屬性(Parameter) 預設值(Default) 描述(Deion)
username   連線資料庫的使用者名稱
password   連線資料庫的密碼
jdbcUrl   同DBCP中的jdbcUrl屬性
driverClassName 根據url自動識別 這一項可配可不配,如果不配置druid會根據url自動識別dbType,然後選擇相應的driverClassName
initialSize 0 初始化時建立物理連線的個數。初始化發生在顯示呼叫init方法,或者第一次getConnection時 *參見DBCP中的initialSize屬性
maxActive 8 最大連線池數量(Maximum number of Connections a pool will maintain at any given time.)

*參見DBCP中的maxTotal屬性

maxIdle 8 已經不再使用,配置了也沒效果

*參見DBCP中的maxIdle屬性

minIdle   最小連線池數量
maxWait   獲取連線時最大等待時間,單位毫秒。配置了maxWait之後,預設啟用公平鎖,併發效率會有所下降,如果需要可以通過配置useUnfairLock屬性為true使用非公平鎖。
poolPreparedState- ments FALSE 是否快取preparedStatement,也就是PSCache。PSCache對支援遊標的資料庫效能提升巨大,比如說oracle。
maxOpenPrepared- Statements -1 要啟用PSCache,必須配置大於0,當大於0時,poolPreparedStatements自動觸發修改為true。 在Druid中,不會存在Oracle下PSCache佔用記憶體過多的問題,可以把這個數值配置大一些,比如說100
testOnBorrow TRUE 申請連線時執行validationQuery檢測連線是否有效,做了這個配置會降低效能。
testOnReturn FALSE 歸還連線時執行validationQuery檢測連線是否有效,做了這個配置會降低效能
testWhileIdle FALSE 建議配置為true,不影響效能,並且保證安全性。申請連線的時候檢測,如果空閒時間大於timeBetweenEvictionRunsMillis,執行validationQuery檢測連線是否有效。
validationQuery   用來檢測連線是否有效的sql,要求是一個查詢語句。如果validationQuery為null,testOnBorrow、testOnReturn、testWhileIdle都不會其作用。在mysql中通常為select 'x',在oracle中通常為select 1 from dual
timeBetweenEviction-RunsMillis   1) Destroy執行緒會檢測連線的間隔時間

2) testWhileIdle的判斷依據

minEvictableIdle- TimeMillis   Destory執行緒中如果檢測到當前連線的最後活躍時間和當前時間的差值大於minEvictableIdleTimeMillis,則關閉當前連線。
removeAbandoned   對於建立時間超過removeAbandonedTimeout的連線強制關閉
removeAbandoned-Timeout   指定連線建立多長時間就需要被強制關閉
logAbandoned FALSE 指定發生removeabandoned的時候,是否記錄當前執行緒的堆疊資訊到日誌中
filters   屬性型別是字串,通過別名的方式配置擴充套件外掛,常用的外掛有: 1)監控統計用的filter:stat 2)日誌用的filter:log4j 3)防禦sql注入的filter:wall

效能比較

最大連線數控制為10,測試獲取10w個連線的時間,單位為ms
這個表是對從第一次獲取連線開始至最後次的統計時間(total)
dbcp druid C3P0
1 8475 655 1621
2 8333 793 1732
3 8291 753 1745
4 8454 642 1581
5 8464 673 1848
6 8307 660 1787
7 8337 699 1808
8 8438 581 1707
平均該表為每次獲取連線時間的統計(sum)
dbcp druid C3P0
1 8305 579 1370
2 8124 698 1443
3 8140 665 1514
4 8284 575 1331
5 8278 596 1463
6 8127 572 1490
7 8155 606 1522
8 8229 509 1439
平均對比三者的效能,druid最快,c3p0還行,dbcp我就不吐槽了和其他兩個不在一個數量級上。
關鍵功能
  Druid DBCP C3P0
LRU
PSCache
PSCache-Oracle-Optimized
ExceptionSorter
監控
擴充套件
對比功能發現,druid的確強大,特別擴充套件功能這塊,druid可以擴充套件很多外掛,包括擴充套件其他連線池。
對比三個連線池發現,druid功能強大,效能高,雖然在複雜環境下穩定性沒有測過,但是後援團(阿里)比較強大,就算有問題也能很快的解決。而且還提供了監控平臺,有助於優化我們的程式碼和sql。並且在擴充套件上支援在連線池層面的sql注入預警。