DBCP连接池泄露问题

线上使用DBCP时,因为网络闪断、数据库重启、授权错误,在获取连接出现异常时,就可能导致连接数持续增多,配置以下参数可以进行补救:

datasource.removeAbandoned=true
datasource.removeAbandonedTimeout=120
datasource.logAbandoned=true 


datasource.removeAbandoned=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.
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.
Setting one or both of these to true can recover db connections from poorly written applications which fail to close connections.
Setting removeAbandonedOnMaintenance to true removes abandoned connections on the maintenance cycle (when eviction ends). This property has no effect unless maintenance is enabled by setting timeBetweenEvicionRunsMillis to a positive value. 

标记是否删除泄露的连接,如果他们超过了removeAbandonedTimout的限制

如果设置为true, 连接被认为是被泄露并且可以被删除,如果空闲时间超过removeAbandonedTimeout. 
设置为true可以为异常没有关闭连接的程序修复数据库连接


datasource.removeAbandonedTimeout=120   

Timeout in seconds before an abandoned connection can be removed.

泄露的连接可以被删除的超时值, 单位秒


datasource.logAbandoned=true 

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.

标记当Statement或连接被泄露时是否打印程序的stack traces日志。
被泄露的Statements和连接的日志添加在每个连接打开或者生成新的Statement,因为需要生成stack trace。


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.

如果开启"removeAbandoned",那么连接在被认为泄露时可能被池回收。这个机制在(getNumIdle() < 2) and (getNumActive() > getMaxActive() - 3)时被触发。

举例当maxActive=20, 活动连接为18,空闲连接为1时可以触发"removeAbandoned"
但是活动连接只有在没有被使用的时间超过"removeAbandonedTimeout"时才被删除,默认300秒

在resultset中游历不被计算为被使用


参考:https://commons.apache.org/proper/commons-dbcp/configuration.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值