Apache-DBCP数据库连接池解读

举报
小工匠 发表于 2021/09/11 00:22:01 2021/09/11
【摘要】 概述配置项说明 基本配置项 usernamepasswordurldriverClassnameconnectionProperties 事务相关配置项数据源链接数量配置...

概述

官网: 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
  
 
  • 1

事务相关配置项

这里写图片描述


数据源链接数量配置项

这里写图片描述

注意: 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)
  
 
  • 1
  • 2
  • 3
  • 4

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.

文章来源: artisan.blog.csdn.net,作者:小小工匠,版权归原作者所有,如需转载,请联系作者。

原文链接:artisan.blog.csdn.net/article/details/78007935

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。