从连接池获得连接,循环数据怎么总报异常

如题所述

上使用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中游历不被计算为被使用。
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答