日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

Apache-DBCP数据库连接池解读

發布時間:2025/3/21 数据库 53 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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数据库连接池解读的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。