Apache-DBCP数据库连接池解读
- 概述
- 配置項說明
- 基本配置項
- username
- password
- url
- driverClassname
- connectionProperties
- 事務相關配置項
- 數據源鏈接數量配置項
- 連接健康情況維護和檢查
- 緩存語句配置項
- 連接泄露回收配置項
- 基本配置項
概述
官網: https://commons.apache.org/proper/commons-dbcp/index.html
commons-dbcp2包依賴于commons-pool2包中的代碼來提供底層對象池機制。
DBCP現在有三種不同的版本來支持不同版本的JDBC。如下所示
DBCP 2 compiles and runs under Java 7 only (JDBC 4.1)
DBCP 1.4 compiles and runs under Java 6 only (JDBC 4)
DBCP 1.3 compiles and runs under Java 1.4-5 only (JDBC 3)
由Java 7運行的應用程序應使用DBCP 2。
由Java 6運行的應用程序應使用DBCP 1.4。
在Java 1.4下運行時應使用DBCP 1.3。
DBCP 2基于Commons Pool 2,與DBCP 1.x相比,提供了更高的性能,JMX支持以及眾多其他新功能。 由于DBCP 2.x與DBCP 1.x不是兼容的,所以升級到2.x的用戶應該知道Java包名稱已經改變,以及Maven坐標。 用戶還應該注意,一些配置選項(例如maxActive to maxTotal)已被重命名.
配置項說明
基本配置項
username
連接的用戶名,通過驅動創建我們需要的連接
password
連接的密碼,通過驅動創建我們所需要的連接.
url
連接的路徑,通過驅動創建我們所需要的連接.
driverClassname
要使用的JDBC驅動程序的完全限定的Java類名稱
connectionProperties
JDBC驅動建立連接時附帶的連接屬性屬性的格式必須為這樣:[屬性名=property;]
注意:”username” 與 “password” 兩個屬性會被明確地傳遞,因此這里不需要包含他們。
比如:
connectionProperties=useUnicode=true;characterEncoding=utf8事務相關配置項
數據源鏈接數量配置項
注意: 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.
連接健康情況維護和檢查
緩存語句配置項
This component has also the ability to pool PreparedStatements. When enabled a statement pool will be created for each Connection and PreparedStatements created by one of the following methods will be pooled:
public PreparedStatement prepareStatement(String sql)public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)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.
連接泄露回收配置項
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.
總結
以上是生活随笔為你收集整理的Apache-DBCP数据库连接池解读的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring JDBC-Spring对D
- 下一篇: C3P0-数据库连接池解读